Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
Finding the Most Recurring Character
function maxRecurringChar(text) {
  let charMap = {};
  let maxCharValue = 0;
  let maxChar = '';
  for (let char of text) {
    if (charMap.hasOwnProperty(char)) {
      charMap[char]++;
    } else {
      charMap[char] = 1;
    }
  }
  for (let char in charMap) {
    if (charMap[char] > maxCharValue) {
      maxCharValue = charMap[char];
      maxChar = char;
    }
  }
  return maxChar;
}

Here is the code I don't understand, cause first for always will return 1 , if always will return 1 why do we need to write the first loop?

Here is the course link

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The algorithm creates a new Object charMap to which all characters of the String text will be added as attributes. In the first for-loop you either add a new attribute with the name of the current char and set the counted value to 1 or, if already present, increment the counted value if the char was already found in the string. So you effectivly count for every char the number of occurences.

In the second for-loop the Object is searched for the greatest counted value, which will be the char with the highest count of occurences.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!