views:

221

answers:

4

I have several array to deal with. I need to extract the most duplicate value from each array.

From [3, 7, 7, 7], I need to find the value 7. Each array size is 4. For now, I don't have to think about when the most duplicate values are more than one such as [3, 7, 7, 7]. All the values are a number.

I looked around the web. I found several ways to make an array to become uniq(). But I haven't found a way to get the duplicate value. I am using jQuery, but raw JavaScript is fine for this task.

+3  A: 

Not perfect in terms of efficiency, but does the job:

var nums = [3, 7, 7, 7];
var freqs = {};
var max_index;
var max_value = -1/0; // Negative infinity.

$.each(nums, function(i, v) {
  if (freqs[v] != undefined) {
    freqs[v]++;
  } else {
    freqs[v] = 1;
  }
});
$.each(freqs, function(num, freq) {
  if (freq > max_value) {
    max_value = freq;
    max_index = num;
  }
});

if (max_index != undefined) {
  alert("Most common element is " + max_index + " with " + max_value + " repetition(s).");
}
​
Max Shawabkeh
+1  A: 

Here's a quick example using just javascript:

function mostFrequent(arr) {
    var uniqs = {};

    for(var i = 0; i < arr.length; i++) {
        uniqs[arr[i]] = (uniqs[arr[i]] || 0) + 1;
    }

    var max = { val: arr[0], num: 1 };
    for(var u in uniqs) {
        if(max.num < uniqs[u]) { max = { val: u, num: uniqs[u] }; }
    }

    return max.val;
}

A quick note on algorithmic complexity -- because you have to look at each value in the array at least once, you cannot do better than O(n). This is assuming that you have no knowledge of the contents of the array. If you do have prior knowledge (e.g. the array is sorted and only contains 1s and 0s), then you can devise an algorithm with a run time that is some fraction of n; though technically speaking, it's complexity is still O(n).

Xavi
+1  A: 

Here's a bit more compact version again using only javascript:

var arr = [3, 7, 7, 7, 10, 10, 8, 5, 5, 5, 5, 20, 20, 1];
var result = {}, max = 0, res;
for(var v in arr) {
  result[arr[v]] = (result[arr[v]] || 0) + 1;
  if(result[arr[v]] > max) { max = result[arr[v]]; res = arr[v]; }
}
alert(res);
Nick Craver
This solution doesn't work. It always returns the last value in the array regardless of its frequency.
Xavi
@Xavi - Good point, I was accessing the object array incorrectly, fixed and a bit more compact/efficient now...though less readable.
Nick Craver
A: 
Array.prototype.mostFreq=function(){
 var what, a= this.concat(), ax, freq,
 count, max=0, limit= a.length/2;
 while(a.length){
  what= a.shift();
  count=1; 
  while((ax= a.indexOf(what))!= -1){
   a.splice(ax,1); // remove counted items  
   ++count;
  }
  // if any item has more than half the array, quit counting
  if(count> limit) return what; 
  if(count> max){
   freq= what;
   max= count;
  }
 }
 return freq;
}
var a=[1,1,2,5,4,2,7,7,1,1,1,3,7,7,3,4,3,7,3,5,6,2,3,1,1,7,7,2,4,3,6,7,6,6]
alert(a.mostFreq())
kennebec