Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
[1,2,3,4,5,4]
[1,2,3,4,5]
[2,2,2,4,4]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3591: Check if Any Element Has Prime Frequency
import java.util.*;
class Solution {
public boolean checkPrimeFrequency(int[] nums) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : nums) {
cnt.merge(x, 1, Integer::sum);
}
for (int x : cnt.values()) {
if (isPrime(x)) {
return true;
}
}
return false;
}
private boolean isPrime(int x) {
if (x < 2) {
return false;
}
for (int i = 2; i <= x / i; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #3591: Check if Any Element Has Prime Frequency
func checkPrimeFrequency(nums []int) bool {
cnt := make(map[int]int)
for _, x := range nums {
cnt[x]++
}
for _, x := range cnt {
if isPrime(x) {
return true
}
}
return false
}
func isPrime(x int) bool {
if x < 2 {
return false
}
for i := 2; i*i <= x; i++ {
if x%i == 0 {
return false
}
}
return true
}
# Accepted solution for LeetCode #3591: Check if Any Element Has Prime Frequency
class Solution:
def checkPrimeFrequency(self, nums: List[int]) -> bool:
def is_prime(x: int) -> bool:
if x < 2:
return False
return all(x % i for i in range(2, int(sqrt(x)) + 1))
cnt = Counter(nums)
return any(is_prime(x) for x in cnt.values())
// Accepted solution for LeetCode #3591: Check if Any Element Has Prime Frequency
fn check_prime_frequency(nums: Vec<i32>) -> bool {
use std::collections::HashMap;
let mut primes = [true; 101];
primes[0] = false;
primes[1] = false;
for i in 2..=100 {
if !primes[i] {
continue;
}
primes[i] = true;
let mut v = i * 2;
while v <= 100 {
primes[v] = false;
v += i;
}
}
let mut h = HashMap::new();
for num in nums {
*h.entry(num).or_insert(0) += 1;
}
for &v in h.values() {
if primes[v as usize] {
return true;
}
}
false
}
fn main() {
let nums = vec![3,0,3,6,3,3];
let ret = check_prime_frequency(nums);
println!("ret={ret}");
}
#[test]
fn test() {
{
let nums = vec![1, 2, 3, 4, 5, 4];
assert!(check_prime_frequency(nums));
}
{
let nums = vec![1, 2, 3, 4, 5];
assert!(!check_prime_frequency(nums));
}
{
let nums = vec![2, 2, 2, 4, 4];
assert!(check_prime_frequency(nums));
}
{
let nums = vec![3, 0, 3, 6, 3, 3];
assert!(!check_prime_frequency(nums));
}
}
// Accepted solution for LeetCode #3591: Check if Any Element Has Prime Frequency
function checkPrimeFrequency(nums: number[]): boolean {
const cnt: Record<number, number> = {};
for (const x of nums) {
cnt[x] = (cnt[x] || 0) + 1;
}
for (const x of Object.values(cnt)) {
if (isPrime(x)) {
return true;
}
}
return false;
}
function isPrime(x: number): boolean {
if (x < 2) {
return false;
}
for (let i = 2; i * i <= x; i++) {
if (x % i === 0) {
return false;
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.