Easy
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 <= 100
0 <= nums[i] <= 100
import kotlin.math.max
class Solution {
private fun isPrime(n: Int): Boolean {
if (n <= 1) {
return false
}
if (n == 2 || n == 3) {
return true
}
for (i in 2..<n) {
if (n % i == 0) {
return false
}
}
return true
}
fun checkPrimeFrequency(nums: IntArray): Boolean {
val n = nums.size
if (n == 1) {
return false
}
var maxi = Int.Companion.MIN_VALUE
for (`val` in nums) {
maxi = max(`val`, maxi)
}
val hash = IntArray(maxi + 1)
for (num in nums) {
hash[num]++
}
for (j in hash) {
if (isPrime(j)) {
return true
}
}
return false
}
}