Javascript Program for Least frequent element in an array
Last Updated :
13 Sep, 2024
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
// 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;
console.log(leastFrequent(arr, n));
Complexity Analysis:
- 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
// JavaScript program to find the least frequent element
// in an array.
function leastFrequent(arr, n) {
// Insert all elements in hash.
let hash = new Map();
for (let 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
let min_count = n + 1, res = -1;
hash.forEach((value, key) => {
if (min_count >= value) {
res = key;
min_count = value;
}
});
return res;
}
// driver program
let arr = [1, 3, 2, 1, 2, 2, 3, 1];
let n = arr.length;
console.log(leastFrequent(arr, n));
Complexity Analysis:
- Time Complexity : O(n)
- Auxiliary Space : O(n)
Please refer complete article on Least frequent element in an array for more details!
Similar Reads
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 : 3Explanation: 3 appears minimum number of times in given array. Input : arr[] = {10, 20, 30}Outp
11 min read
Javascript Program for Range Queries for Frequencies of array elements Given an array of n non-negative integers. The task is to find frequency of a particular element in the arbitrary range of array[]. The range is given as positions (not 0 based indexes) in array. There can be multiple queries of given type. Examples: Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11}; lef
2 min read
Top K Frequent Elements in an Array Given an array arr[] and a positive integer k, the task is to find the k most frequently occurring elements from a given array.Note: If more than one element has same frequency then prioritise the larger element over the smaller one.Examples: Input: arr= [3, 1, 4, 4, 5, 2, 6, 1], k = 2Output: [4, 1]
15+ min read
Most frequent element in an array Given an array, the task is to find the most frequent element in it. If there are multiple elements that appear a maximum number of times, return the maximum element.Examples: Input : arr[] = [1, 3, 2, 1, 4, 1]Output : 1Explanation: 1 appears three times in array which is maximum frequency.Input : a
10 min read
PHP Second most frequent element in an array Given an array we have to find the second most frequent element present in it. Examples: Input : array(3, 3, 4, 5, 5, 5, 9, 8, 8, 8, 8, 8); Output : Second most frequent element is: 5 Input : array("geeks", "for", "geeks"); Output : Second most frequent element is: for Here are some common approache
4 min read