Find most common character in within a string?

Hey guys!

Need some help trying to determine what character occurs most frequently in a textarea. Not sure how to do this, since I am a JS noob.
Do I really have to store every single character in an array and then sort it? Is there a Regular Expression for this?
Thanks in advance! :wink:

Something like this? http://www.kirby98.fsnet.co.uk/frequency.htm

Aye, pretty much like that except that i need to count all possible characters and return the most frequent one. I took a look at the script you posted, but it’s a little inconvenient for my purpose, cause it’s declaring variables for every letter. Thanks though! :slight_smile:

/*
You can use a regular expression, but you’d need to use exec,
and go over the same string again for every different character,
and still have to juggle the matches.

I’d split the string into characters and join it back sorted.
A regular expression can then match all the sets of
consecutive duplicate letters in one pass.
*/

function lettercount(s, force){
	var cs= [], L,
	s= ta.value.toLowerCase().split('').sort().join('');
	var M= s.match(/([a-z])\\1+/g)
	M.sort(function(a, b){
		return a.length> b.length? -1: 1;
	});
	if(force) return M;

	L= M[0].length;
	while(M.length && M[0].length== L){
		cs.push(M.shift().charAt(0));
	}
	return cs.join(' and ')+' occurred '+L+' times.'
}

[B]var ta={
value: 'The quick red fox jumps over the lazy brown dog\‘s back’
};

alert(lettercount(ta.value));[/B]
/* returned value: (String)
o and e occurred 4 times.
*/

Sorry- there was an error I didn’t spot while I could edit the post.

function lettercount(s, force){
	var cs= [], L,
	s= s.toLowerCase().split('').sort().join('');
	var M= s.match(/([a-z])\\1+/g)
	M.sort(function(a, b){
		return a.length> b.length? -1: 1;
	});
	if(force) return M;

	L= M[0].length;
	while(M.length && M[0].length== L){
		cs.push(M.shift().charAt(0));
	}
	return cs.join(' and ')+' occurred '+L+' times.'
}

I can’t think of a reason why you would need to do this client-side.

Yeah, well I bet you think it’s summer time, too.

If you want to avoid sorting and regexps, you can build an associative array dynamically, storing the count of each character and recording the highest as you go:


<html>
<head>
<title>Most Common Character</title>
<script type="text/javascript">

function mcc( elem )
{
 var charTotals = [], allChars = elem.value.split(''), 
     len = allChars.length, mostChar = null, mostCount = 0;  
   
 for( var i = 0, ch ; i < len; i++ )
 {
  ch =  allChars[ i ]; 
  
  charTotals[ ch ] = charTotals[ ch ] ? charTotals[ ch ] + 1 : 1;  
  
  if( charTotals[ ch ] > mostCount )
  {
   mostCount = charTotals[ ch ] ;
   mostChar = ch;
  }
 }
 
 alert('Most common character: ' + (mostChar ? '\\'' + mostChar +'\\'\
\
Occurrences: ' + mostCount : 'None') ); 
 
 return mostChar;
}

</script>
</head>
<body>

<textarea id='ta'></textarea>

<input type='button' value = 'go' onclick='mcc( document.getElementById("ta") )'>

</body>
</html>

This counts all characters and in the event of a tie, displays the earliest occurring. Should be slightly easier to explain to your teacher.

ok :shifty: I just find a lot of the time scripts people ask for are not needed at all.

thanks alot guys… works just fine :slight_smile: