BackeasyHashingAccenture

Count Unique Frequencies Solution

Problem Statement

Given a list of integers representing mineral frequencies, determine the count of distinct frequencies in the list.

Example 1
Input
[1, 2, 2, 3, 3, 3]
Output
3

Explanation: Step-by-step: with input [1, 2, 2, 3, 3, 3], we count the frequency of each number. The frequency of 1 is 1, the frequency of 2 is 2, and the frequency of 3 is 3. Therefore, the count of distinct frequencies is 3.

Example 2
Input
[4, 4, 4, 4]
Output
1

Explanation: Step-by-step: with input [4, 4, 4, 4], we count the frequency of each number. The frequency of 4 is 4. Therefore, the count of distinct frequencies is 1.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of only lowercase English letters.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Count Unique Frequencies — Problem Statement & Solution Guide

HashingEasyFrequency Map
TimeO(n)
|
SpaceO(n)

Problem Description

Given a list of integers representing mineral frequencies, determine the count of distinct frequencies in the list.

Examples

Example 1

Input

[1, 2, 2, 3, 3, 3]

Output

3

Explanation: Step-by-step: with input [1, 2, 2, 3, 3, 3], we count the frequency of each number. The frequency of 1 is 1, the frequency of 2 is 2, and the frequency of 3 is 3. Therefore, the count of distinct frequencies is 3.

Example 2

Input

[4, 4, 4, 4]

Output

1

Explanation: Step-by-step: with input [4, 4, 4, 4], we count the frequency of each number. The frequency of 4 is 4. Therefore, the count of distinct frequencies is 1.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of only lowercase English letters.

Optimal Approach & Strategy

The optimized approach would involve using a hash set to store unique minerals, resulting in a time complexity of O(n).

Brute Force Approach

The brute force approach would involve sorting the list of mineral frequencies and then counting the number of unique minerals by iterating through the sorted list.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums) { let freq = {}; for (let num of nums) { if (freq[num]) { freq[num]++; } else { freq[num] = 1; } }; let uniqueFreq = new Set(); for (let num in freq) { uniqueFreq.add(freq[num]); }; return uniqueFreq.size; }

Asked in Top Tech Interviews

Accenture

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.