You are given an integer array nums.
Return true if the frequency of any element of the array is prime, otherwise, return false.
The frequency of an element x is the number of times it occurs in the array.
A prime number is a natural number greater than 1 with only two factors, 1 and itself.
Example 1:
Input: nums = [1,2,3,4,5,4]
Output: true
Explanation:
4 has a frequency of two, which is a prime number.
Example 2:
Input: nums = [1,2,3,4,5]
Output: false
Explanation:
All elements have a frequency of one.
Example 3:
Input: nums = [2,2,2,4,4]
Output: true
Explanation:
Both 2 and 4 have a prime frequency.
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 100Problem Overview: You are given an integer array and need to determine whether any value appears a prime number of times. The task reduces to counting element frequencies and checking if at least one frequency is a prime number.
The key observation: the array values themselves do not matter. Only the frequency of each unique element matters. Once you compute those counts, the problem becomes a simple prime number check.
Approach 1: Frequency Map + Prime Check (O(n + k√f) time, O(k) space)
Use a hash map to count how many times each element appears. Iterate through the array once and update a frequency table. After building the map, iterate through the frequency values and check whether any count is prime.
A number x is prime if it has no divisors between 2 and √x. For each frequency, run this check. The moment you find a prime count, return true. If none of the counts are prime, return false. Counting takes O(n) time, and each prime check costs O(√f) where f is the frequency value. Space complexity is O(k) where k is the number of unique elements.
This approach relies on standard frequency counting using a hash table and a basic primality test from number theory. It works well because the number of unique elements is usually much smaller than n.
Approach 2: Frequency Counting + Sieve Precomputation (O(n + m log log m) time, O(m) space)
If you expect many prime checks, you can precompute prime numbers using the Sieve of Eratosthenes. First count element frequencies using a map or array. Track the maximum frequency m. Then run a sieve up to m to mark all prime numbers.
After building the sieve, simply check whether any frequency value corresponds to a prime index in the sieve array. This converts repeated primality checks into constant-time lookups. The sieve step costs O(m log log m) time and O(m) space, where m is the maximum frequency.
This version can be useful when the dataset is large or when prime checks are frequent. The approach combines array traversal with efficient counting and prime preprocessing.
Recommended for interviews: The hash map counting with a direct prime check is the expected solution. It demonstrates clean use of frequency counting and a standard primality test while keeping the implementation short. A brute-force counting approach shows basic understanding, but the hash map + prime check method highlights stronger problem decomposition and is typically what interviewers want.
We use a hash table cnt to count the frequency of each element. Then, we iterate through the values in cnt and check if any of them is a prime number. If there is a prime, return true; otherwise, return false.
The time complexity is O(n times \sqrt{M}), and the space complexity is O(n), where n is the length of the array nums and M is the maximum
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Frequency Map + Prime Check | O(n + k√f) | O(k) | General case; simplest and most common interview solution |
| Frequency Counting + Sieve of Eratosthenes | O(n + m log log m) | O(m) | When many prime checks are required or maximum frequency is known |
LeetCode#3591 Check if Any Element Has Prime Frequency - Python • CodeJulian • 911 views views
Watch 9 more video solutions →Practice Check if Any Element Has Prime Frequency with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor