Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How can I get the most repeated value from an Array in javascript?

This is my array

var data = [
    { values: "Number of pips" }, 
    { values: 4 }, 
    { values: 4 }, 
    { values: 4 }, 
    { values: 5 }, 
    { values: 2 }, 
    { values: 6 }, 
    { values: 6 },
    { values: 5 }
];

The result should be 4, how can I get this?

I have tried this, taken from Get the element with the highest occurrence in an array

function mode(array)
{
    if(array.length == 0)
        return null;
    var modeMap = {};
    var maxEl = array[0], maxCount = 1;
    for(var i = 0; i < array.length; i++)
    {
        var el = array[i];
        if(modeMap[el] == null)
            modeMap[el] = 1;
        else
            modeMap[el]++;    
        if(modeMap[el] > maxCount)
        {
            maxEl = el;
            maxCount = modeMap[el];
        }
    }
    return maxEl;
}

but this is return 6, not 4.

share|improve this question
5  
I don't believe you have tried anything at all... –  musefan Aug 14 at 9:03
    
have you tried anything on your own? –  Bhushan Kawadkar Aug 14 at 9:04
1  
check this stackoverflow.com/questions/2440295/… –  dhidy Aug 14 at 9:07

3 Answers 3

up vote 3 down vote accepted

I Got This

var arr= [{ values: "Number of pips" }, { values: 4 }, { values: 4 }, { values: 4 }, { values: 5 }, { values: 2 }, { values: 6 }, { values: 6 }, { values: 5 }];



    var uniqs = {};

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

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

    alert(max.val);

DEMO

share|improve this answer
    
this will give you the 4 –  cracker Aug 14 at 9:16

The problem with your attempted code is that you do not have an array of numbers, you have an array of objects. If you want to count the most values then you have to use that value, rather than the whole object.

In regards to the code you have attempted, you just need to change the following line:

var el = array[i].values;

Here is the full code:

function mode(array)
{
    if(array.length == 0)
        return null;
    var modeMap = {};
    var maxEl = array[0], maxCount = 1;
    for(var i = 0; i < array.length; i++)
    {
        var el = array[i].values;// This is the change.
        if(modeMap[el] == null)
            modeMap[el] = 1;
        else
            modeMap[el]++;  
        if(modeMap[el] > maxCount)
        {
            maxEl = el;
            maxCount = modeMap[el];
        }
    }
    return maxEl;
}

Here is a working example

share|improve this answer
  1. Sort the array. (I assume you can do that... else copy it first on another array)
  2. iterate the array and by keeping the previouslyVisited item work this out
share|improve this answer
2  
Firstly, don't encourage people to ask questions when they haven't tried to solve the problem themselves. Secondly, your answer doesn't provide a solution. Seriously, you think telling the OP to "work this out" is helpful? I think this would have been better as a comment, then the OP could attempt to code your recommendations –  musefan Aug 14 at 9:09
    
@musefan , i have update my full code –  user256103 Aug 14 at 9:12
    
@musefan I disagree with your "don't encourage people to ask questions when they haven't tried to solve the problem " I agree my answer could have been a comment. downgrading me was just nasty –  Zo72 2 days ago
    
@Zo72: But SO is against people asking question without showing any attempt to solve the problem, it's in the guides. The minimum is to at least include current code... anyway, that's not why you got the downvote. I downvoted because your answer doesn't provide a solution to the problem, it's just a suggestion on how one might be able to solve the problem –  musefan 2 days ago
    
@musefan given the sketchy question (at the time of my response at least) I gave a sketchy solution. How do I count duplicates in my array ? well sort the array and then iterate over... I thought it was fair... Anyway you had the right to downvote me so I accept that –  Zo72 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.