Find duplicates in an array in Kotlin
This article explores different ways to find duplicates in an array in Kotlin.
1. Using a Set
A simple solution is to loop through the array and keep track of all visited elements in a Set. If an element is encountered before, mark it as duplicate. Finally, report all duplicates after processing each element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
fun findAllDuplicates(array: IntArray): Set<Int> { val seen: MutableSet<Int> = mutableSetOf() val duplicates: MutableSet<Int> = mutableSetOf() for (i in array) { if (!seen.add(i)) { duplicates.add(i) } } return duplicates } fun main() { val values = intArrayOf(4, 3, 2, 3, 1, 4, 5) val duplicates = findAllDuplicates(values) println(duplicates) // [3, 4] } |
The code can be easily shortened using the filter() function:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
fun findAllDuplicates(array: IntArray): Set<Int> { val seen: MutableSet<Int> = mutableSetOf() return array.filter { !seen.add(it) }.toSet() } fun main() { val values = intArrayOf(4, 3, 2, 3, 1, 4, 5) val duplicates = findAllDuplicates(values) println(duplicates) // [3, 4] } |
2. Using filter() function
Another solution is to filter duplicate elements in the array using the filter() function with the count() function. This logic would translate to the following code:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
fun findAllDuplicates(array: IntArray): Set<Int> { val nums = array.toList() return nums.filter { item -> nums.count { it == item } > 1 }.toSet() } fun main() { val values = intArrayOf(4, 3, 2, 3, 1, 4, 5) val duplicates = findAllDuplicates(values) println(duplicates) // [4, 3] } |
3. Using a Frequency Map
The above implementation calls the count() function for each array element, which will lead to poor performance for large arrays. A better solution is to create a frequency map and filter all values with a frequency greater than 1.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun findAllDuplicates(array: IntArray): Set<Int> { val frequencies = mutableMapOf<Int, Long>() array.forEach { frequencies.merge(it, 1L) { a, b -> a + b } } return frequencies.keys.filter { frequencies[it]!! > 1 }.toSet() } fun main() { val values = intArrayOf(4, 3, 2, 3, 1, 4, 5) val duplicates = findAllDuplicates(values) println(duplicates) // [3, 4] } |
Alternatively, we can use the groupBy() library function to group values by the key, such that we get a map where each group key is associated with a list of the corresponding mapping. Then the problem reduces to filtering the values with the size more than 1 and returning their keys. Here’s what the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fun findAllDuplicates(array: IntArray): Set<Int> { return array.mapIndexed { index, i -> i to index } .groupBy { it.first } .filterValues { it.size > 1 } .keys } fun main() { val values = intArrayOf(4, 3, 2, 3, 1, 4, 5) val duplicates = findAllDuplicates(values) println(duplicates) // [4, 3] } |
That’s all about finding duplicates in an array in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)