Open In App

JavaScript Program to Find k Most Occurrences in the Given Array

Last Updated : 31 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

K most occurrences in an array refer to finding the K unique elements that appear the most frequently within the given array, where K is a specified integer. These are the elements with the highest frequencies in the array.

Example:

Input: arr[] = {3, 1, 4, 4, 5, 2, 6, 1}, K = 2
Output: 4 1
Explanation:
Frequency of 4 = 2, Frequency of 1 = 2
These two have the maximum frequency and 4 is larger than 1.
refer
Input: arr[] = {7, 10, 11, 5, 2, 5, 5, 7, 11, 8, 9}, K = 4
Output: 5 11 7 10
Explanation:
Frequency of 5 = 3, Frequency of 11 = 2, Frequency of 7 = 2, Frequency of 10 = 1
These four have the maximum frequency and 5 is largest among rest.

Approach 1: Using map() method

In This approach we are using the map() method to count the occurrences of elements in an array, creating a frequency map. It then converts this map into an array of [element, frequency] pairs, sorts them by frequency in descending order, and finally, extracts the top k elements, providing an efficient solution for finding k most occurring elements.

Syntax:

map((element) => { /* … */ })

Example: Below is the implementation of the above approach.

Javascript




function frequentElements(arr, k) {
    const frequencyMap = new Map();
      
    // Count the occurrences of 
    // each element in the array
    for (let i = 0; i < arr.length; i++) {
        const num = arr[i];
        frequencyMap.has(num)
            ? frequencyMap.
                set(num, frequencyMap.get(num) + 1)
            : frequencyMap.set(num, 1);
    }
      
    // Sort the entries in the frequency 
    // map by frequency in descending order
    const sortedEntries = 
        [...frequencyMap.entries()]
        .sort((a, b) => b[1] - a[1]);
    const result = [];
      
    // Extract the first k elements 
    // from the sorted entries
    for (let i = 0; i < k && 
            i < sortedEntries.length; i++) {
            result.push(sortedEntries[i][0]);
    }
    return result;
}
const arr = [3, 1, 4, 4, 5, 2, 6, 1];
const k = 2;
const kMostFrequent = frequentElements(arr, k);
console.log(...kMostFrequent);


Output

1 4

Approach 2: Using reduce() with sort() method

In This approach uses the reduce() method to create a frequency map of elements in the array, followed by sorting the map entries using sort(). Finally, it extracts the top k elements from the sorted entries to find the k most frequent elements.

Syntax:

array.reduce( function(total, currentValue, currentIndex, arr), 
initialValue )

Example: Below is the implementation of the above approach.

Javascript




function frequentElements(arr, k) {
    const frequencyMap =
        arr.reduce((map, num) => {
            map.set(num, (map.get(num) || 0) + 1);
            return map;
        }, new Map());
          
    // Sort the entries in the frequency 
    // map by frequency in descending order
    const sortedEntries =
        [...frequencyMap.entries()]
            .sort((a, b) => b[1] - a[1]);
    const result = [];
    for (const [element, _] of
        sortedEntries.slice(0, k)) {
        result.push(element);
    }
    return result;
}
const arr = [3, 1, 4, 4, 5, 2, 6, 1];
const k = 2;
const kMostFrequent = 
    frequentElements(arr, k);
console.log(...kMostFrequent);


Output

1 4


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads