Check if Any Element Has Prime Frequency - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [4,6,4,6,4]
Output: true
💡 Note: Element 4 appears 3 times and 3 is prime. Element 6 appears 2 times and 2 is prime. Since both frequencies are prime, return true.
Example 2 — No Prime Frequencies
$ Input: nums = [1,1,1,1]
Output: false
💡 Note: Element 1 appears 4 times. 4 is not prime (divisible by 1, 2, and 4), so return false.
Example 3 — Single Element
$ Input: nums = [5]
Output: false
💡 Note: Element 5 appears 1 time. 1 is not prime (prime numbers must be greater than 1), so return false.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • -1000 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Check if Any Element Has Prime Frequency46464Count frequencies:4 → freq: 36 → freq: 2Is 3 prime? Yes! (only divisors: 1, 3)Is 2 prime? Yes! (only divisors: 1, 2)Output: trueAt least one element has prime frequency
Understanding the Visualization
1
Input
Array [4,6,4,6,4] with repeated elements
2
Process
Count frequencies: 4 appears 3 times, 6 appears 2 times
3
Output
Check primality: 3 is prime, 2 is prime → return true
Key Takeaway
🎯 Key Insight: Count element frequencies efficiently, then check each frequency for primality using square root optimization
Asked in
Google 15 Amazon 12 Microsoft 8
8.5K Views
Medium Frequency
~12 min Avg. Time
342 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen