Sunday, October 6, 2024
Google search engine
HomeData Modelling & AIJavascript Program for Least frequent element in an array

Javascript Program for Least frequent element in an array

Given an array, find the least frequent element in it. If there are multiple elements that appear least number of times, print any one of them.
Examples : 
 

Input : arr[] = {1, 3, 2, 1, 2, 2, 3, 1}
Output : 3
3 appears minimum number of times in given
array.

Input : arr[] = {10, 20, 30}
Output : 10 or 20 or 30

 

A simple solution is to run two loops. The outer loop picks all elements one by one. The inner loop finds frequency of the picked element and compares with the minimum so far. Time complexity of this solution is O(n2)
A better solution is to do sorting. We first sort the array, then linearly traverse the array.
 

Javascript




<script>
  
// JavaScript program to find the least frequent element
// in an array.
  
    function leastFrequent(arr, n)
    {
            
        // Sort the array
        arr.sort();
        
        // find the min frequency using 
        // linear traversal
        let min_count = n+1, res = -1;
        let curr_count = 1;
            
        for (let i = 1; i < n; i++) {
            if (arr[i] == arr[i - 1])
                curr_count++;
            else {
                if (curr_count < min_count) {
                    min_count = curr_count;
                    res = arr[i - 1];
                }
                    
                curr_count = 1;
            }
        }
        
        // If last element is least frequent
        if (curr_count < min_count)
        {
            min_count = curr_count;
            res = arr[n - 1];
        }
        
        return res;
    }
  
// Driver code
  
        let arr = [1, 3, 2, 1, 2, 2, 3, 1];
        let n = arr.length;
        document.write(leastFrequent(arr, n));
                                 
</script>


Output: 

3

 

Time Complexity : O(n Log n) 
Auxiliary Space : O(1)
An efficient solution is to use hashing. We create a hash table and store elements and their frequency counts as key value pairs. Finally we traverse the hash table and print the key with minimum value.
 

Javascript




<script>
  
// JavaScript program to find the least frequent element
// in an array.
  
function leastFrequent(arr, n)
{
    // Insert all elements in hash.
    var hash = new Map();
    for (var i = 0; i < n; i++)
    {
        if(hash.has(arr[i]))
            hash.set(arr[i], hash.get(arr[i])+1)
        else
            hash.set(arr[i], 1);
    }
  
    // find the min frequency
    var min_count = n+1, res = -1;
  
    hash.forEach((value, key) => {
          
        if (min_count >= value) {
            res = key;
            min_count = value;
        }
    });
  
    return res;
}
  
// driver program
var arr = [1, 3, 2, 1, 2, 2, 3, 1];
var n = arr.length;
document.write( leastFrequent(arr, n));
  
</script>


Output: 

3

 

Time Complexity : O(n) 
Auxiliary Space : O(n)
 

Please refer complete article on Least frequent element in an array for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments