Add contains duplicate solution
This commit is contained in:
parent
07d0b4945f
commit
ed6a2c39be
|
@ -1,3 +1,4 @@
|
|||
fun main() {
|
||||
println("Hello, Kotlin/Native!")
|
||||
// println(containsDuplicate(intArrayOf(1, 23, 34, 1)))
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package space.dezentrale.prgrnd
|
||||
fun containsDuplicate(nums: IntArray): Boolean {
|
||||
val unicates: MutableSet<Int> = mutableSetOf()
|
||||
nums.forEach {
|
||||
if (unicates.contains(it)) {
|
||||
return true
|
||||
} else {
|
||||
unicates.add(it)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package space.dezentrale.prgrnd
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ContainsDuplicateTest {
|
||||
@Test
|
||||
fun `it returns true for '1_2_3_1'`() {
|
||||
assertTrue(containsDuplicate(intArrayOf(1, 2, 3, 1)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `it returns false for '1_2_3_4'`() {
|
||||
assertFalse(containsDuplicate(intArrayOf(1, 2, 3, 4)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `it returns true for '1_1_1_3_3_4_3_2_4_2'`() {
|
||||
assertTrue(containsDuplicate(intArrayOf(1, 1, 1, 3, 3, 4, 3, 2, 4, 2)))
|
||||
}
|
||||
}
|
Reference in New Issue