diff --git a/kt/longest-square-streak-in-an-array.kt b/kt/longest-square-streak-in-an-array.kt new file mode 100644 index 0000000..dd0fbbf --- /dev/null +++ b/kt/longest-square-streak-in-an-array.kt @@ -0,0 +1,15 @@ +class Solution { + fun longestSquareStreak(nums: IntArray): Int { + nums.sort() + + val foundBest = mutableMapOf() + for (num in nums) { + val squared = num * num + foundBest.put(squared, 1 + foundBest.getOrDefault(num, 0)) + } + + return foundBest.values.filter { + it >= 2 + }.maxOrNull() ?: -1 + } +}