Javascript program for counting frequencies of array elements
In this article, we are given an array that may contain duplicate values. We will print all elements and their frequencies if the duplicates exist. We can do this by using two methods:
- Running two loops
- Using Hashing
- Using the Binary search function
Examples:
Input : arr[] = {10, 20, 20, 10, 10, 20, 5, 20}
Output : 10 3
20 4
5 1
Input : arr[] = {10, 20, 20}
Output : 10 1
20 2
A simple solution is to run two loops. For every item count number of times, it occurs. To avoid duplicate printing, keep track of processed items
Example 1: Below is the implementation of the above approach:
Javascript
<script>
function countFreq(arr, n) {
let visited = Array.from({ length: n }, (_, i) => false );
for (let i = 0; i < n; i++) {
if (visited[i] == true )
continue ;
let count = 1;
for (let j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
visited[j] = true ;
count++;
}
}
console.log(arr[i] + " " + count);
}
}
let arr = [10, 20, 20, 10, 10, 20, 5, 20];
let n = arr.length;
countFreq(arr, n);
</script>
|
Output:
10 3
20 4
5 1
Time Complexity: O(n2), where n time for each loop.
Auxiliary Space: O(n), for visited array.
Example 2: An efficient solution is to use hashing:
Javascript
<script>
function countFreq(arr, n) {
var mp = new Map();
for ( var i = 0; i < n; i++) {
if (mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i]) + 1)
else
mp.set(arr[i], 1)
}
var keys = [];
mp.forEach((value, key) => {
keys.push(key);
});
keys.sort((a, b) => a - b);
keys.forEach((key) => {
console.log(key + " " + mp.get(key));
});
}
var arr = [10, 20, 20, 10, 10, 20, 5, 20];
var n = arr.length;
countFreq(arr, n);
</script>
|
Output:
5 1
10 3
20 4
Example 3: In the above efficient solution, how to print elements in the same order as they appear in input:
Javascript
<script>
function countFreq(arr, n) {
var mp = new Map();
for ( var i = 0; i < n; i++) {
if (mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i]) + 1)
else
mp.set(arr[i], 1)
}
for ( var i = 0; i < n; i++) {
if (mp.get(arr[i]) != -1) {
console.log(arr[i] + " " + mp.get(arr[i]));
mp.set(arr[i], -1);
}
}
}
var arr = [10, 20, 20, 10, 10, 20, 5, 20];
var n = arr.length;
countFreq(arr, n);
</script>
|
Output:
10 3
20 4
5 1
Another Efficient Solution (Space optimization): we can find the frequency of array elements using the Binary search function. First, we will sort the array for binary search. Our frequency of element will be ‘(last occ – first occ)+1’ of an element in an array.
Example:
Javascript
function countFreq(arr, n) {
arr.sort((a, b) => a - b);
let i = 0;
while (i < n) {
const first_index = arr.indexOf(arr[i]);
const last_index = arr.lastIndexOf(arr[i]);
i = last_index;
const fre = last_index - first_index + 1;
console.log(arr[i] + " " + fre);
i++;
}
}
const arr = [10, 20, 20, 10, 10, 20, 5, 20];
countFreq(arr, arr.length);
|
Time Complexity: O(n*log2n), where O(log2n) time for the binary search function.
Auxiliary Space: O(1)
Last Updated :
27 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...