1

I am following this Counting the occurrences / frequency of array elements to find the occurrence of each item in the array. If any item occurs more than or equal to 3 times, then I have to show an alert.

Below is the code I have tried:

var countries = ['India', 'Australia', 'Bangladesh', 'India', 'India',
  'New Zealand', 'New Zealand', 'Pakistan', 'Pakistan', 'West Indies'];

var recipientsArray = countries.sort();

var reportRecipientsDuplicate = [];
for (var i = 0; i < recipientsArray.length; ++i) {
  if (!reportRecipientsDuplicate[recipientsArray[i]]) {
    reportRecipientsDuplicate[recipientsArray[i]] = 0;
  }
  ++reportRecipientsDuplicate[recipientsArray[i]];
}

How to check if any item has occurred more than or equal to 3 times? Thanks

Henke
  • 4,445
  • 3
  • 31
  • 44
venkat14
  • 583
  • 3
  • 12
  • 34

3 Answers3

4

You can use reduce method for a easy way to solve this problem. reduce method creates a new array or object by applying a provided callback function.

The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

var countries = ['India' , 'Australia' , 'Bangladesh' , 'India' , 'India' , 'New Zealand' , 'New Zealand' , 'Pakistan' , 'Pakistan' , 'West Indies'];
var statistics=countries.reduce(function(obj,elem){
    obj[elem]=obj[elem] || 0;
    obj[elem]++;
    return obj;
},{});
for(country in statistics){
  if(statistics[country]>=3)
    console.log(country);
} 
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
1

const array = ['India' , 'Australia' , 'Bangladesh' , 'India' , 'India' , 'New Zealand' , 'New Zealand' , 'Pakistan' , 'Pakistan' , 'West Indies'];


_.each(_.uniq(_.filter(array, function(v) {
    return _.filter(array, function(v1) {
        return v1 === v;
    }).length > 2;
})), function(i) {
    console.log(i);
});
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

Using UnderscoreJS:

_.uniq(_.filter(array, v => 
  _.filter(array, v1 => v1 === v).length > 2));

Code

_.each(_.uniq(_.filter(array, function(v) {
    return _.filter(array, function(v1) {
        return v1 === v;
    }).length > 2;
})), function(i) {
    console.log(i);
});

Demo https://jsfiddle.net/sumitridhal/e9yf3txg/3/

Sumit Ridhal
  • 1,359
  • 3
  • 14
  • 30
1

Try this code:

var countries = ["India" , "Australia" , "Bangladesh" , "India" , "India" , "New Zealand" , "New Zealand" , "Pakistan" , "Pakistan" , "West Indies","New Zealand"];

var recipientsArray = countries.sort(); 
checkedWords = [];

for (var i = 0; i < recipientsArray.length; ++i) {
    var count = 0;

  for(var j=i;j<recipientsArray.length;j++){
    if(checkedWords.indexOf(recipientsArray[i]) > -1){
        continue;
    }
    if(recipientsArray[i] == recipientsArray[j]){
        count = count+1;
    }
  }
  console.log(count);
  if(count >= 3){
    alert(recipientsArray[i]);
  }
  checkedWords.push(recipientsArray[i]);
}

https://jsfiddle.net/74bzzze3/

I hope this answers your question :)

nikhalster
  • 481
  • 2
  • 9