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
Unique number of occurrences of elements in an array in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the first and the only argument.
The function should check whether all the integers that are present in the array appear for unique number of times or not.
If they do, the function should return true, false otherwise.
For example −
If the input array is −
const arr = [7, 5, 5, 8, 2, 4, 7];
Then the output should be −
false
because both the integers 7 and 5 appears for 2 times each.
We will first use a hash map to map integers to their frequencies(occurrences) and then use that map to build a set that stores unique frequencies.
Approach
The solution works in two steps:
- Count the frequency of each element using a hash map
- Use a Set to check if any frequency appears more than once
Example
Following is the code −
const arr = [7, 5, 5, 8, 2, 4, 7];
const uniqueAppearances = (arr = []) => {
const map = {};
const set = new Set();
// Count frequencies
for(let i = 0; i < arr.length; i++){
const el = arr[i];
map[el] = (map[el] || 0) + 1;
}
// Check for duplicate frequencies
for(key in map){
const value = map[key];
if(set.has(value)){
return false;
}
set.add(value);
}
return true;
};
console.log(uniqueAppearances(arr));
Output
false
Step-by-Step Breakdown
Let's trace through the example:
const arr = [7, 5, 5, 8, 2, 4, 7];
// Step 1: Count frequencies
const frequencies = {};
// After counting: {7: 2, 5: 2, 8: 1, 2: 1, 4: 1}
console.log("Element frequencies:");
for(let i = 0; i < arr.length; i++){
const el = arr[i];
frequencies[el] = (frequencies[el] || 0) + 1;
}
console.log(frequencies);
// Step 2: Check frequency uniqueness
const frequencySet = new Set();
console.log("Frequency values:", Object.values(frequencies));
Element frequencies:
{ '2': 1, '4': 1, '5': 2, '7': 2, '8': 1 }
Frequency values: [ 1, 1, 2, 2, 1 ]
Alternative Implementation
Here's a more concise version using modern JavaScript features:
const uniqueOccurrences = (arr) => {
const frequencies = {};
// Count frequencies
arr.forEach(num => frequencies[num] = (frequencies[num] || 0) + 1);
// Check if all frequencies are unique
const freqValues = Object.values(frequencies);
return freqValues.length === new Set(freqValues).size;
};
// Test cases
console.log(uniqueOccurrences([1,2,2,1,1,3])); // true (1:3, 2:2, 3:1)
console.log(uniqueOccurrences([1,2])); // false (1:1, 2:1)
console.log(uniqueOccurrences([7,5,5,8,2,4,7])); // false (5:2, 7:2)
true false false
Key Points
- Use a hash map to count element frequencies
- Use a Set to detect duplicate frequency values
- The algorithm has O(n) time complexity where n is array length
- Space complexity is O(n) for the frequency map and set
Conclusion
This approach efficiently determines if all elements in an array have unique occurrence counts by using frequency mapping and Set-based duplicate detection. The two-step process ensures optimal performance for this problem.
