Created
August 6, 2020 22:22
-
-
Save UNISERVO1/117c07ed0e8eab6e3ac27a89bcffff84 to your computer and use it in GitHub Desktop.
August LeetCoding Challenge Week 1: Day 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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