forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleNumberIII.kt
31 lines (28 loc) · 1.01 KB
/
SingleNumberIII.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package questions
import _utils.UseCommentAsDocumentation
import utils.assertIterableSameInAnyOrder
/**
* Given an integer array nums, in which exactly two elements appear only once and
* all the other elements appear exactly twice.
* Find the two elements that appear only once. You can return the answer in any order.
*
* [Source](https://leetcode.com/problems/single-number-iii/)
*/
@UseCommentAsDocumentation
private fun singleNumber(nums: IntArray): IntArray {
if (nums.size == 2) return nums
val record = mutableSetOf<Int>()
for (i in 0..nums.lastIndex) {
val element = nums[i]
if (record.contains(element)) {
record.remove(element)
} else {
record.add(element)
}
}
return intArrayOf(record.elementAt(0), record.elementAt(1))
}
fun main() {
assertIterableSameInAnyOrder(intArrayOf(3, 5).toList(), singleNumber(intArrayOf(1, 2, 1, 3, 2, 5)).toList())
assertIterableSameInAnyOrder(listOf(-1, 0), singleNumber(intArrayOf(-1, 0)).toList())
}