easyArraysPattern: Hashing

Unique Signal Frequencies Solution

Problem Statement

Given an array of integers, count the number of unique frequencies present in the array.

Examples

Example 1:
Input:[1, 2, 2, 3, 3, 3]
Output:3
Explanation: The number of unique frequencies.
Example 2:
Input:[4, 4, 4, 4, 5, 5, 5, 5]
Output:2
Explanation: The number of unique frequencies.
Example 3:
Input:[1, 1, 1, 1, 1]
Output:1
Explanation: The number of unique frequencies.

Constraints

  • The input array contains non-negative integers.
  • The array is not empty.
  • The array may contain duplicate elements.
Time: O(n) Space: O(n)
Use a hash set to store unique frequencies. Iterate through the input array, add each frequency to the hash set, and finally return the size of the hash set. This approach has a time complexity of O(n).

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

from collections import Counter def unique_frequencies(arr): if not arr: return 0 counts = Counter(arr) return len(set(counts.values()))