Answers for "count the frequency of each element of an array in java using hashmap"

1

Count frequency of array elements js

var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]

const map = arr.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());

console.info([...map.keys()]) // to get unique elements
console.info([...map.values()]) // to get the occurrences
console.info([...map.entries()]) // to get the pairs [element, frequency]
Posted by: Guest on August-18-2020
2

mark occurances of elements in array cpp

int sumOfDistinct(int a[], int n){
  	int sum = 0; 
    for (int i = 0; i < n; i++) { 
      	 // If element appears first time 
        if (a[abs(a[i]) - 1] >= 0) { 
            sum += abs(a[i]); 
            a[abs(a[i]) - 1] *= -1; 
        } 
    } 
    return sum;
Posted by: Guest on May-02-2020
0

frequency count in java using hashmap

static void countFreq(int arr[], int n)
    {
        Map<Integer, Integer> mp = new HashMap<>();
 
        // Traverse through array elements and
        // count frequencies
        for (int i = 0; i < n; i++)
        {
            if (mp.containsKey(arr[i]))
            {
                mp.put(arr[i], mp.get(arr[i]) + 1);
            }
            else
            {
                mp.put(arr[i], 1);
            }
        }
        // Traverse through map and print frequencies
        for (Map.Entry<Integer, Integer> entry : mp.entrySet())
        {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }
Posted by: Guest on August-15-2021

Code answers related to "count the frequency of each element of an array in java using hashmap"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language