Link to home
Start Free TrialLog in
Avatar of BlueEdger
BlueEdger

asked on

Determining Most Common Value in an Array

Hello,

Is there anyway I can determine the most common value in an array?

Say my array contains 1,1,2,2,2,2,3

How do I get that 2 is the most common value??

Is this at all possible?

Thanks for your help.
Avatar of avner
avner

Here is an example of something like that :

<html>
<head>
<title>about:blank</title>
<script language="javascript1.2">
<!-- copyright(c) avcoh@yahoo.com
function calculateWords(sName)
{
var oField = document.getElementById(sName);
var sString = oField.innerText;
sString =sString.replace(","," ");
sString =sString.replace(/\n|\r/g," ");
sString =sString.replace("  "," ");
var arr = new Object();
var arrWords = sString.split(" ");
for (var i=0;i<arrWords.length;i++)
      {
      var sWord = arrWords[i];

            if (arr[sWord])
            {
                  arr[sWord]=arr[sWord]+1;
            }
            else
            {
                  arr[sWord] = 1
            }
      }

var s = "Total of words in textarea :\n";
for (var i in arr)
      {
            s+="["+i+"] count is :" + arr[i] + "\n";
      }

alert(s);

}
-->
</script>
<style>

</style>
</head>
<body  onload="calculateWords('ta')"
<textarea id="ta" cols="40" rows="30">
this is a list of this is ah words
this is a list of this is ah words
this is a list of this is ah words
this is a list of this is ah words
this is a list of this is ah words
this is a list of this is ah words
this is a list of this is ah words
this is a list of this is ah words</textarea>
</body>
</html>
Avatar of Zvonko
Like this:

<script>

var myArray = ["Atext","Atext","Btext","Btext","Btext","Btext","Ctext"];

alert(mC(myArray));


function mC(theArray){
  tempArray = theArray.slice(0,theArray.length);
  tempArray.sort()
  mVal = aVal = "";
  mCnt = aCnt = 0;
  for(i=0;i<tempArray.length;i++){
    if(aVal!=theArray[i]){
      if(aCnt>mCnt){
        mCnt = aCnt;
        mVal = aVal
      }
      aVal = theArray[i];
      aCnt = 1;
    } else {
      aCnt++;
    }
  }
  return [mVal, mCnt];
}
</script>

The text values are only for better visulisation.
Of course does the mC() function work also for digits:

<script>

var myArray = [1,1,2,2,2,2,3];

alert(mC(myArray));


function mC(theArray){
  tempArray = theArray.slice(0,theArray.length);
  tempArray.sort()
  mVal = aVal = "";
  mCnt = aCnt = 0;
  for(i=0;i<tempArray.length;i++){
    if(aVal!=theArray[i]){
      if(aCnt>mCnt){
        mCnt = aCnt;
        mVal = aVal
      }
      aVal = theArray[i];
      aCnt = 1;
    } else {
      aCnt++;
    }
  }
  return [mVal, mCnt];
}
</script>

Zvonko, your code is incorrect :


Try to have this array :
var myArray = ["Atext","Atext","Btext","Btext","Btext","Btext","Ctext", "Atext", "Atext", "Atext"];
Avatar of BlueEdger

ASKER

Zvonko,

My array contains 0.445,0.445,0.783,0.783,0.783,0.881

Your mC function is returning ,0

If I step through the tempArray I get the individual values but the last one is this ,0

aVal returns the correct 0.783, but then also ,0

I cant work out why it is doing that??
Thanks, it was a typo. I did not use the sorted tempArray.

Here the correction:

<script>

var myArray = ["Atext","Atext","Btext","Btext","Btext","Btext","Ctext", "Atext", "Atext", "Atext"];

alert(mC(myArray));


function mC(theArray){
  tempArray = theArray.slice(0,theArray.length);
  tempArray.sort()
  mVal = aVal = "";
  mCnt = aCnt = 0;
  for(i=0;i<tempArray.length;i++){
    if(aVal!=tempArray[i]){
      if(aCnt>mCnt){
        mCnt = aCnt;
        mVal = aVal
      }
      aVal = tempArray[i];
      aCnt = 1;
    } else {
      aCnt++;
    }
  }
  return [mVal, mCnt];
}
</script>

BlueEdger, try please again. Now it works: 0.783, 3 times.
I've tried it with one array where the last four values are all the same, and I get the right result.  But when I tried it where all the values where the same except for the last one, then I get last value,1.

Can you test your function with this criteria and see if you get the same thing??
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This version works now even with one single array element.
Thanks Zvonko, that works a treat.  Avner, Im sorry I did not test your solution as Zvonko's function was neater, and hence I am awarding Zvonko the points as that was the solution I used.  
Thanks for the points and for your feedback.

@avner: thank you for helping me finding errors.
Here are your points: http:Q_20871628.html
Uhps, sorry I though this was a five hundred pointer!
Anyway, take it :)
just 4 fun
<script language="javascript">
      var myArray = ["Atext","Btext","Btext","Btext","Btext","Btext","Ctext", "Atext", "Atext","Ctext"];
      var c = new Array();
      for (var i=0; i<myArray.length; i++)
            c[""+myArray[i]]=isNaN(c[""+myArray[i]])?0:c[""+myArray[i]]+1;
      var mi=""+myArray[0];
      for (i in c)
            if (c[mi]<c[i])
                  mi=i;
      alert(mi);
</script>

:)
Don
Nice idea, the same as from avner: using associative arrays.
But with same beginning errors like mine: you did not test it :)
Sorry twixt, I did test it wrong. Your method works correct.