-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Pairless.kt
36 lines (31 loc) · 896 Bytes
/
Pairless.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
32
33
34
35
36
/*
* Think of a perfect world where everybody has a soulmate.
* Now, the real world is imperfect: there is exactly one number in the array
* that does not have a pair. A pair is an element with the same value.
* For example in this array:
* 1, 2, 1, 2
* every number has a pair, but in this one:
* 1, 1, 1
* one of the ones is lonely.
*
* Your task is to implement the findPairless() function so that it finds the
* lonely number and returns it.
*
* A hint: there's a solution that looks at each element only once and uses no
* data structures like collections or trees.
*/
package pairless
fun findPairless(a: IntArray): Int {
// Write your solution here
return 0
}
/** Solution 1
INPUT OUTPUT
A B A XOR B
0 0 0 OK
0 1 1
1 0 1
1 1 0 OK */
fun findPairless(a: IntArray): Int {
return a.reduce { a, b -> a xor b }
}