JavaScript Program for the Least Frequent Element in an Array


In this program, we are given an array of integers or elements and we have to return the element which presents the least numbers of the time in the array, and if there is more than one least frequent number present we can return any one of them which we are going to see in the article below.

Introduction to Problem

In the given problem we have to find the least frequent element in an array. Here we have given an array with duplicate numbers and here we have to count the occurrence of each element and among all of them which have the least occurrence that number we have to return. If more than one element has the least frequency then we can return any one of them. Let’s see some examples of them-

For example,

We are given an array of size num

Input:
Num = 8
Array = {1, 3, 3, 5, 5, 4, 4, 4}
Output: 1 

It presents minimum numbers of time i.e 1 time among all the elements as 3 present 2 times, 5 present 2 times, and 4 present 3 times.)

Let’s see another example in which we have multiple elements present the least number of times.

Input:
Num =  2
Array = {3, 1}
Output:
3 or 1 (as both 1 and 2 present 1 time)

Approach 1: Brute Force

In this approach, we have run nested for loop. The first forloop is for considering each element one by one, and the second forloop is for calculating frequencies for that each element and maintaining an element count which stores the frequencies of elements and is updated every time for new elements and leastCountNum is storing the num which has the least count.

Example

JavaScript program for the least frequent element in an array.

function leastFrequent(array, num){
   var countMin = num+1;
   var count = 0;
   var leastCountNum = -1;
   for (var i = 0; i < num; i++) {
      count = 0;
      for(let j = 0; j < num; j++){
         if(array[i] == array[j]){
            count++;
         }
      }
      if(count < countMin){
         countMin = count;
         leastCountNum = array[i];
      }
   }
   return leastCountNum;
}
var num = 8;
var array = [1, 3, 3, 5, 5, 4, 4, 4];
console.log("Least Frequent Value is: "+ leastFrequent (array, num) );

Time Complexity is O(N2) where N is the size of the array.

Space Complexity is O(1)

Now see the optimized solution of it.

Approach 2: Using the Sort function

Here we first sort the array then linearly traverses the array and check the frequency count of the element on the basis of it updating the leastCountNum.

Example

function leastFrequent(array, num){
   array.sort(); //sort an array using sort function
   
   // find the min frequency using
   // linear traversal
   
   var countMin = num+1, leastCountNum = -1;
   var count = 1;
   for (var i = 1; i < num; i++) {
      if (array[i] == array[i - 1]){
         count++;
      } else {
         if (count < countMin) {
            countMin = count;
            leastCountNum = array[i - 1];
         }
         count=1;
      }
   }
   
   // checking for the last element is least frequent or not
   if (count < countMin) {
      countMin = count;
      leastCountNum = array[num - 1];
   }
   return leastCountNum;
}
var num = 8;
var array = [2, 3, 3, 5, 5, 4, 4, 4];
console.log("Least Frequent Value is: "+ leastFrequent (array, num) );

Time Complexity is O(NlogN) where N is the size of the array.

Space Complexity is O(1).

Approach 3: Using Hash Map

In this approach, as key value pairs, we store elements and their frequency counts in a hash table. Then traversing the hash table and print the key with the lowest value.

Example

function leastFrequent(array, num){
   var hash = new Map();
   for (var i = 0; i < num; i++) {
      if(hash.has(array[i]))
      hash.set(array[i], hash.get(array[i])+1)
      else
      hash.set(array[i], 1);
   }
   
   // find the least frequent value uding hash map function
   var countMin = num+1,leastCountNum = -1;
   hash.forEach((value, key) => {
      if (countMin >= value) {
         countMin = value;
         leastCountNum = key;
      }
   });
   return leastCountNum;
}
var num = 8;
var array = [1, 3, 3, 5, 5, 4, 4, 4];
console.log("The least Frequent Value is: "+ leastFrequent (array, num) );

Time Complexity is O(N) where N is the size of the array.

Space Complexity is O(N) where N is the size of the array.

Conclusion

In this tutorial, we have learned to find the least frequent value of an array using three approaches i.e brute force with time complexity O(N*N) and constant space complexity, using the sort function with time complexity of O(N*log(N)), and using the hash map function with the time complexity of O(N) and space complexity of O(N).

Updated on: 30-Mar-2023

402 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements