0

How do I return only the letter that repeats the most times? How to make it return only the result e = 5 in the following case

enter code here
var s = "Teeeeessttt"
var x = (s.toLowerCase());
function count() {
array_elements = ([...x]);;
array_elements.sort();

var current = null;
var cnt = 0;
for (var i = 0; i < array_elements.length; i++) {
    if (array_elements[i] != current) {
        if (cnt > 0) {
            document.write(current + ' - ' + cnt + '<br>');
        }
        current = array_elements[i];
        cnt = 1;
    } else {
        cnt++;
    }
}
if (cnt > 0) {
    document.write(current + ' - ' + cnt);
}
}

count();
dylo98
  • 3
  • 2
  • This would return `['e', 5]`, `[...[...s].reduce((a,v)=>a.set(v,a.get(v)+1||1),new Map())].sort((a,b)=>b[1]-a[1])[0]` – Keith Oct 27 '20 at 14:09
  • 1
    Does this answer your question? [Finding the most frequent character in a string javascript](https://stackoverflow.com/questions/22590023/finding-the-most-frequent-character-in-a-string-javascript) – Sarun UK Oct 27 '20 at 14:20

2 Answers2

1

First you would need to count the amount of times a character occurs. You could use the reduce method to loop over each character and store that character as a key in an object. The value of the key should represent the amount of times the character has occurred.

It would look like the object below.

{
  "t": 4,
  "e": 5,
  "s": 2
}

From here you want to look for the highest number and return the key that corresponds to it. I've borrowed the solution of this SO thread to get the highest count of the object using another reduce loop.

const string = "Teeeeessttt";

const mostOccuringCharacter = string => {
  // Count the occurence of each character.
  const count = [...string.toLowerCase()].reduce((counter, char) => {
    counter[char] = (counter[char] || 0) + 1;
    return counter;
  }, {});
    
  // Compare the values with each other and return the
  // character with the highest count.
  const highestChar = Object.keys(count).reduce((a, b) => 
    count[a] > count[b] ? a : b);
    
  // Return an array with the character and the count.
  return [highestChar, count[highestChar]]
};

const [ char, count ] = mostOccuringCharacter(string);

console.log(`${char} = ${count}`);
Emiel Zuurbier
  • 19,095
  • 3
  • 17
  • 32
0

@UKS points out a dupe for this, interestingly there was a post with issues with funcky unicode.. eg. , but no solution was shown for this.

So with that in mind I thought I would knock up a version that handles unicode nicely. I've also split into multiple functions for re-use. As you can see it's also easy to alter to find the least used character if so wanted. Not sure if this will handle 100% unicode, there might be some even more funcky ones. :)

function unicodeStrSplit(str) {
  const x = [...str];
  //let handle some funcky unicode
  for (let p=x.length-1;p>0;p--)
    if(x[p].charCodeAt(0)===0xd83c){x[p-1] += x[p], x.splice(p, 1) };
  return x;
}

function arrayCounter(arr) {
  return [...arr.reduce((a,v)=>a.set(v,a.get(v)+1||1),new Map())].
    sort((a,b) => b[1]-a[1]);
}

function stringCounter(str) {
  return arrayCounter(unicodeStrSplit(str));
}


console.log(stringCounter("Teeeeessttt").shift()); //first array item
//funcky unicode too.
console.log(stringCounter('').shift()); //first array item
//also let's get the least used
console.log(stringCounter('').pop()); //last array item
Keith
  • 22,005
  • 2
  • 27
  • 44