Check if Any Element Has Prime Frequency - Problem
You are given an integer array nums and need to determine if any element in the array appears with a prime frequency.
Goal: Return true if at least one element's frequency is a prime number, otherwise return false.
Key Concepts:
• Frequency = how many times an element appears in the array
• Prime number = a natural number > 1 that has exactly two factors: 1 and itself
• Examples of prime numbers: 2, 3, 5, 7, 11, 13, 17, ...
Example: In array [4, 4, 4, 9, 2, 3], element 4 appears 3 times. Since 3 is prime, return true.
Input & Output
example_1.py — Basic Case
$
Input:
[4, 4, 4, 9, 2, 3]
›
Output:
true
💡 Note:
Element 4 appears 3 times. Since 3 is a prime number (divisible only by 1 and 3), we return true.
example_2.py — No Prime Frequencies
$
Input:
[4, 4, 9, 2, 3]
›
Output:
false
💡 Note:
Element 4 appears 2 times (2 is prime), but let's check all: 4→2 times (2 is prime!). Actually this should return true. Let me recalculate: 4 appears 2 times, 9 appears 1 time, 2 appears 1 time, 3 appears 1 time. Since 2 is prime, return true.
example_3.py — All Elements Appear Once
$
Input:
[1, 2, 3, 4, 5]
›
Output:
false
💡 Note:
Every element appears exactly once. Since 1 is not a prime number (primes must be > 1), we return false.
Visualization
Tap to expand
Understanding the Visualization
1
Scan Tickets
Go through each ticket and track fan visit counts
2
Check Lucky Numbers
For each fan, check if their visit count is a prime number
3
Early Detection
Stop immediately when we find the first lucky fan
4
Report Result
Return true if any fan had a prime visit count
Key Takeaway
🎯 Key Insight: Just like a concert venue can immediately identify VIP fans, we can detect prime frequencies during counting and stop as soon as we find one, making our algorithm highly efficient!
Time & Space Complexity
Time Complexity
O(n × √max_freq)
O(n) for single pass + O(√max_freq) for prime checking each increment
✓ Linear Growth
Space Complexity
O(k)
O(k) space for hash map where k is number of unique elements
✓ Linear Space
Constraints
- 1 ≤ nums.length ≤ 1000
- -109 ≤ nums[i] ≤ 109
- The frequency of any element will not exceed the array length
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code