How to count duplicate value in an array in javascript

前端 未结 28 1315
后悔当初
后悔当初 2020-11-22 06:07

Currently, I got an array like that:

var uniqueCount = Array();

After a few steps, my array looks like that:

uniqueCount =          


        
相关标签:
28条回答
  • 2020-11-22 06:17

    Declare an object arr to hold the unique set as keys. Populate arr by looping through the array once using map. If the key has not been previously found then add the key and assign a value of zero. On each iteration increment the key's value.

    Given testArray:

    var testArray = ['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a'];
    

    solution:

    var arr = {};
    testArray.map(x=>{ if(typeof(arr[x])=="undefined") arr[x]=0; arr[x]++;});
    

    JSON.stringify(arr) will output

    {"a":3,"b":2,"c":2,"d":2,"e":2,"f":1,"g":1,"h":3}
    

    Object.keys(arr) will return ["a","b","c","d","e","f","g","h"]

    To find the occurrences of any item e.g. b arr['b'] will output 2

    0 讨论(0)
  • 2020-11-22 06:18

    You can do something like that:

    uniqueCount = ['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a'];
    var map = new Object();
    
    for(var i = 0; i < uniqueCount.length; i++) {
     if(map[uniqueCount[i]] != null) {
        map[uniqueCount[i]] += 1;
    } else {
        map[uniqueCount[i]] = 1;
        }
    }
    

    now you have a map with all characters count

    0 讨论(0)
  • 2020-11-22 06:18
    var uniqueCount = ['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a'];
    // here we will collect only unique items from the array
    var uniqueChars = [];
    
    // iterate through each item of uniqueCount
    for (i of uniqueCount) {
    // if this is an item that was not earlier in uniqueCount, 
    // put it into the uniqueChars array
      if (uniqueChars.indexOf(i) == -1) {
        uniqueChars.push(i);
      } 
    }
    // after iterating through all uniqueCount take each item in uniqueChars
    // and compare it with each item in uniqueCount. If this uniqueChars item 
    // corresponds to an item in uniqueCount, increase letterAccumulator by one.
    for (x of uniqueChars) {
      let letterAccumulator = 0;
      for (i of uniqueCount) {
        if (i == x) {letterAccumulator++;}
      }
      console.log(`${x} = ${letterAccumulator}`);
    }
    
    0 讨论(0)
  • 2020-11-22 06:20
    public class CalculateCount {
    public static void main(String[] args) {
        int a[] = {1,2,1,1,5,4,3,2,2,1,4,4,5,3,4,5,4};
        Arrays.sort(a);
        int count=1;
        int i;
        for(i=0;i<a.length-1;i++){
            if(a[i]!=a[i+1]){
                System.out.println("The Number "+a[i]+" appears "+count+" times");
                count=1;                
            }
            else{
                count++;
            }
        }
        System.out.println("The Number "+a[i]+" appears "+count+" times");
    
    }   
    

    }

    0 讨论(0)
  • 2020-11-22 06:21

    Something like this:

    uniqueCount = ["a","b","c","d","d","e","a","b","c","f","g","h","h","h","e","a"];
    var count = {};
    uniqueCount.forEach(function(i) { count[i] = (count[i]||0) + 1;});
    console.log(count);

    Use a simple for loop instead of forEach if you don't want this to break in older browsers.

    0 讨论(0)
  • 2020-11-22 06:22
    
    // Initial array
    let array = ['a', 'b', 'c', 'd', 'd', 'e', 'a', 'b', 'c', 'f', 'g', 'h', 'h', 'h', 'e', 'a'];
    
    // Unique array without duplicates ['a', 'b', ... , 'h']
    let unique = [...new Set(array)];
    
    // This array counts duplicates [['a', 3], ['b', 2], ... , ['h', 3]] 
    let duplicates = unique.map(value => [value, array.filter(str => str === value).length]);
    
    0 讨论(0)
提交回复
热议问题