Algorithm 101: 6 Ways to Find the Most Recurring Character in a String
Tags: algorithms, javascript, string, tutorial
Published: 2020-03-04 22:43:06

Can you find the most recurring character in a given string?

Today's episode of Algorithm 101 features 6 Ways to <code> Find the Most Recurring Character in a String </code>

```javascript

mostRecurringChar("samson"); // "s"

mostRecurringChar("njoku samson ebere"); // "e"

```

### Prerequisite
This article assumes that you have basic understanding of javascript's string, array and object methods.

### Let's Find the Most Recurring Character using:

* for...in, for...of, hasOwnProperty() and if...statement

```javascript
      function mostRecurringChar(text) {
        let charMap = {};
        let maxValue = 0;
        let maxKey = "";

        for (char of text) {
          if (charMap.hasOwnProperty(char)) {
            charMap[char]++;
          } else {
            charMap[char] = 1;
          }
        }

        for (char in charMap) {
          if (charMap[char] > maxValue) {
            maxValue = charMap[char];
            maxKey = char;
          }
        }

        return maxKey;
      }
```

* for...in, for...Loop, hasOwnProperty() and if...statement

```javascript
      function mostRecurringChar(text) {
        let charMap = {};
        let maxValue = 0;
        let maxKey = "";

        for (let i = 0; i <= text.length; i++) {
          if (charMap.hasOwnProperty(text[i])) {
            charMap[text[i]]++;
          } else {
            charMap[text[i]] = 1;
          }
        }

        for (char in charMap) {
          if (charMap[char] > maxValue) {
            maxValue = charMap[char];
            maxKey = char;
          }
        }

        return maxKey;
      }
```

* for...of, if...statement, hasOwnProperty(), Object.keys(), Object.values(), indexOf() and Math.max()

```javascript
      function mostReoccuringChar(text) {
        let charMap = {};
        let charMapKeys = [];
        let charMapValues = [];
        let maxValue = 0;

        for (char of text) {
          if (charMap.hasOwnProperty(char)) {
            charMap[char]++;
          } else {
            charMap[char] = 1;
          }
        }

        charMapKeys = Object.keys(charMap);
        charMapValues = Object.values(charMap);
        maxValue = Math.max(...charMapValues);

        return charMapKeys[charMapValues.indexOf(maxValue)];
      }
```

* for...Loop, if...statement, hasOwnProperty(char), Object.keys(), Object.values(), indexOf() and Math.max()

```javascript
      function mostReoccuringChar(text) {
        let charMap = {};
        let charMapKeys = [];
        let charMapValues = [];
        let maxValue = 0;

        for (let i = 0; i <= text.length; i++) {
          if (charMap.hasOwnProperty(text[i])) {
            charMap[text[i]]++;
          } else {
            charMap[text[i]] = 1;
          }
        }

        charMapKeys = Object.keys(charMap);
        charMapValues = Object.values(charMap);
        maxValue = Math.max(...charMapValues);

        return charMapKeys[charMapValues.indexOf(maxValue)];
      }
```

* for...of, if...statement, hasOwnProperty(char), Object.entries

```javascript
      function mostReoccuringChar(text) {
        let charMap = {};
        let charMapEntries = [];
        let maxValue = 0;
        let maxKey = "";

        for (char of text) {
          if (charMap.hasOwnProperty(char)) {
            charMap[char]++;
          } else {
            charMap[char] = 1;
          }
        }

        charMapEntries = Object.entries(charMap);

        for (entry of charMapEntries) {
          if (entry[1] > maxValue) {
            maxValue = entry[1];
            maxKey = entry[0];
          }
        }

        return maxKey;
      }
```

* for...Loop, if...statement, hasOwnProperty(char), Object.entries

```javascript
      function mostReoccuringChar(text) {
        let charMap = {};
        let charMapEntries = [];
        let maxValue = 0;
        let maxKey = "";

        for (let i = 0; i <= text.length; i++) {
          if (charMap.hasOwnProperty(text[i])) {
            charMap[text[i]]++;
          } else {
            charMap[text[i]] = 1;
          }
        }

        charMapEntries = Object.entries(charMap);

        for (entry of charMapEntries) {
          if (entry[1] > maxValue) {
            maxValue = entry[1];
            maxKey = entry[0];
          }
        }

        return maxKey;
      }
```

### Conclusion
There are many ways to solve problems programmatically. You are only limited by your imagination. I will love to know other ways you solved yours in the comment section.

If you have questions, comments or suggestions, please drop them in the comment section.

> Up Next: [Algorithm 101: 8 Ways to Capitalize a Sentence](https://dev.to/ebereplenty/algorithm-101-8-ways-to-capitalize-a-sentence-3bia)

You can also follow and message me on social media platforms. 

**[Twitter](https://twitter.com/eberetwit) | [LinkedIn](https://www.linkedin.com/in/samson-ebere-njoku-profile/) | [Github](https://github.com/EBEREGIT)**

Thank You For Your Time.