2

I'm new to JavaScript so please help me on this problem: I have array and I want to get the number of same values. My array is:

 var arr = ["red", "blue", "green", "red", "red", "gray"];

I want to output 3 because I have 3 elements red.

This is what I have done so far:

var numberOfSameElements = 0;
var arr = ["red", "blue", "green", "red", "red", "gray"];
for(var i = 1 ; i <arr.length;i++){
if(arr[i] === arr[i-1]){
   numberOfSameElements++;  
 }
}

  console.log(numberOfSameElements);

I keep getting 1. Please tell me what I am doing wrong. Thank you so much!

HenryDev
  • 4,685
  • 5
  • 27
  • 64

4 Answers4

3

You can reduce the array to a hash map to figure out how many of each word there are:

var words = ["red", "blue", "green", "red", "red", "gray"];
var wordCounts = words.reduce(function(counts, word) {
  counts[word] = (counts[word] || 0)++;
}, { });

console.log(wordCounts);

// >> {
//      red: 3,
//      blue: 1,
//      green: 1,
//      gray: 1
//    }

If you want the name of any words that are duplicated, you can filter the original array down:

var repeatedWords = words.filter(function(word) {
  return wordCounts[word] > 1;
});

console.log(repeatedWords);

// >> ['red']
rrowland
  • 2,734
  • 2
  • 17
  • 32
2

Here you've used one loop and checked your current value with it's previous value. But you need actually two loops.

First one will pick a value. And second one will match the picked value with all others. I've used a found flag that checks if a duplicate value has already been found or not. Try this way,

var numberOfSameElements = 0;
var found = false;
var arr = ["red", "blue", "green", "red", "red", "gray"];
for(var i = 0 ; i <arr.length;i++){
    for(var j = 0; j < arr.length; j++){
        if(arr[i] === arr[j] && i != j){
            if(!found){
                numberOfSameElements++;
                found = true;
            }
        }
    }
    found = false;
}

console.log(numberOfSameElements);

jsFiddle

Md Ashaduzzaman
  • 4,032
  • 2
  • 18
  • 34
2

If you want to get the max number of duplicates find in the array, you first have to count them all. Then find the maximum repetition number (fiddle):

function getLargestNumberOfSame(arr) {
    var counterMap = arr.reduce(function (sameMap, item) { // create a map of same values
        if (!sameMap[item]) {
            sameMap[item] = 1;
        } else {
            sameMap[item]++;
        }

        return sameMap;
    }, {});

    var maxValues = Object.keys(counterMap).map(function(key) {  // turn the map into array of max values
        return counterMap[key];
    });

    return Math.max.apply(window, maxValues); // return the maximum value
}
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
1
 var numberOfSameElements = 1;
var arr = ["red", "blue", "green", "red", "red", "gray"];
arr = arr.sort();
var last;
for(var i = 0 ; i <arr.length -1 ;i++) {
    //console.log(arr[i],arr[i+1]);
if(arr[i] == arr[i+1]){
   numberOfSameElements++;
   last = arr[i];

 } else { 
    console.log(arr[i],"repert",numberOfSameElements,"times");
    numberOfSameElements=1;
   }

}
console.log(last,"repert",numberOfSameElements,"times");
Nitesh singh
  • 915
  • 11
  • 21