LeetCode/kt/longest-square-streak-in-an-array.kt

16 lines
381 B
Kotlin
Raw Permalink Normal View History

class Solution {
fun longestSquareStreak(nums: IntArray): Int {
nums.sort()
val foundBest = mutableMapOf<Int, Int>()
for (num in nums) {
val squared = num * num
foundBest.put(squared, 1 + foundBest.getOrDefault(num, 0))
}
return foundBest.values.filter {
it >= 2
}.maxOrNull() ?: -1
}
}