Skip to content

Instantly share code, notes, and snippets.

@UNISERVO1
Created August 6, 2020 22:22
Show Gist options
  • Save UNISERVO1/117c07ed0e8eab6e3ac27a89bcffff84 to your computer and use it in GitHub Desktop.
Save UNISERVO1/117c07ed0e8eab6e3ac27a89bcffff84 to your computer and use it in GitHub Desktop.
August LeetCoding Challenge Week 1: Day 6
class Solution {
fun findDuplicates(nums: IntArray): List<Int> {
fun twiceFound(uniques: MutableMap<Int, Int>, num: Int): MutableMap<Int, Int> {
uniques.set(num, uniques.getOrPut(num){ 0 } + 1)
return uniques
}
return nums.fold(mutableMapOf<Int, Int>(), { t, num -> twiceFound(t, num) }).toMap().filterValues{ it == 2 }.keys.toList()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment