Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for the Least Frequent Element in an Array
In this program, we are given an array of integers and need to return the element that appears the least number of times. If multiple elements have the same minimum frequency, we can return any one of them.
Problem Statement
Given an array with duplicate numbers, we need to count the occurrence of each element and return the one with the least frequency. If multiple elements have the same minimum frequency, any one can be returned.
Example 1:
Input: [1, 3, 3, 5, 5, 4, 4, 4] Output: 1 Explanation: 1 appears 1 time, 3 appears 2 times, 5 appears 2 times, 4 appears 3 times
Example 2:
Input: [3, 1] Output: 3 or 1 Explanation: Both elements appear only once
Approach 1: Brute Force
This approach uses nested loops to count the frequency of each element. For every element, we traverse the entire array to count its occurrences.
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));
Least Frequent Value is: 1
Time Complexity: O(N²) where N is the size of the array
Space Complexity: O(1)
Approach 2: Using Sort Function
First sort the array, then traverse it linearly to count consecutive identical elements. This groups same elements together, making frequency counting easier.
function leastFrequent(array, num) {
array.sort(); // Sort array to group identical elements
var countMin = num + 1;
var 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;
}
}
// Check the last element
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));
Least Frequent Value is: 2
Time Complexity: O(N log N) due to sorting
Space Complexity: O(1)
Approach 3: Using Hash Map
Store elements and their frequencies as key-value pairs in a Map. Then traverse the Map to find the element with minimum frequency.
function leastFrequent(array, num) {
var hash = new Map();
// Count frequencies
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 element with minimum frequency
var countMin = num + 1;
var 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));
The least Frequent Value is: 1
Time Complexity: O(N) where N is the size of the array
Space Complexity: O(N) for storing frequencies in the Map
Comparison
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Brute Force | O(N²) | O(1) | Small arrays, space-constrained |
| Sort + Linear | O(N log N) | O(1) | Medium arrays |
| Hash Map | O(N) | O(N) | Large arrays, optimal time |
Conclusion
The Hash Map approach provides the best time complexity O(N) for finding the least frequent element. For space-constrained environments, the sorting approach offers a good balance between time and space efficiency.
