JavaScript program for counting frequencies of array elements


Counting the frequencies means we have to count the number of times an element from the array appears in the given array. We can use some inbuilt data structures like maps to get the frequency or we can sort the array also to get the frequency of the array elements. We will discuss both approaches, let’s see both of them one by one −

Sorting the Array

In this approach, we are going to sort the array and check if the current element is the same as the previous one, if the current array is not the same then this is the new element, and the frequency of the previous element is until the count that is a variable which we will use to increase the count of elements.

Approach

  • First, we will sort the array using the inbuilt sort method.

  • We will create an array that will store the elements and their respective frequencies in the given array.

  • We will create a variable ‘count’ to store the number of times the current element occurs.

  • We will iterate over the array and at each iteration, we will check if the current element is equal to the previous element or not.

  • If the current element is equal to the previous one, then we will increase the value of the count.

  • If the current element is not equal to the previous element, then we will store the count with the previous element as a key-pair in the array indicating the frequency of the current element.

  • Also, we will update the value of the count to 1.

  • After iterating over the array we will store the frequency of the last element of the sorted array as it will not be stored and loop ended.

Example

Let’s see the code to implement the above-given approach and add and understand that in a better way.

// given array 
var arr = [ 1, 4, 5, 6, 2, 2, 2, 4, 5, 5, 4, 6, 9, 1, 2, 2, 3]

// sorting the array 
arr.sort()

var count = 1 

for(var i = 1;i<arr.length; i++){
   if(arr[i] == arr[i-1]) {
      count++;
   }
   else {
      console.log("The frequency of "+ arr[i-1] + " is: " + count);
      count = 1;
   }
}

console.log("The frequency of "+ arr[arr.length-1] + " is: " + count);

Time and Space Complexity

The time complexity of the above code is O(N*log(N)) because we have sorted the array and for that time take is N*log(N) and we have traversed over the array once takes O(N) time, where N is the number of elements present in the given array.

The space complexity of the above code is O(1) because we are not using any extra space but if we want to store the frequency then there will be some extra space will be O(N).

Frequency of all Elements Using Maps

Maps are the data structures that store the values in the form of key pairs and data can be updated later. Adding or updating data in the map takes logarithmic time but there will be no need of sorting the array means we don’t have to change the array as we were doing in the previous program. Let’s see the approach first then we will move to the coding part −

Approach

  • First, we will create the map using the new keyword.

  • We will iterate over the array and for each element, we will check.

  • If the current element is present in the map then we will increase the value stored for the current element which is frequency.

  • If the element is not stored then we will add it to the map as a key and will give a value of 1.

  • After iterating over the array we can print the values stored in the map as key-value pairs.

Example

We have seen the approach of the code, now let’s move to the implementation part to get a better understanding of the code −

// given array 
var arr = [ 1, 4, 5, 6, 2, 2, 2, 4, 5, 5, 4, 6, 9, 1, 2, 2, 3]
var map = new Map()
for(var i = 0;i<arr.length; i++){
   if(map.has(arr[i])){
      var k = map.get(arr[i]);
      map.delete(arr[i]);
      map.set(arr[i],k+1)
   }
   else{
      map.set(arr[i],1);
   }
}
console.log(map)

Time and Space Complexity

The time complexity of the above code is O(N*log(N)) where N is the size of the array and the factor or log is due to the working of the map. The space complexity of the above code is O(N) which is needed to store the elements in the map.

Using maps to find the frequency is good because we don’t have to change the given array.

Conclusion

In this tutorial, we are gone through the JavaScript program for counting the frequencies of array elements. Counting the frequencies means we have to count the number of times an element from the array appears in the given array. We have seen two methods for the given problem one is by sorting the elements using the inbuilt sort function and another is done by using the inbuilt map data structure.

Updated on: 24-Mar-2023

506 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements