How to use XOR in Kotlin
find the single number in the array that every element has a duplicate except one.
var a = 0
for (i in numsArray){
a = a xor i
}
return a
eg. input = [2,2,1] out = 1
It's an extension that can be invoked on any Boolean
. You can use it like this:
true.xor(false)
or this:
true xor false
The last one works since the function is defined as infix
.
Other similar extensions defined on Boolean
are and
, or
and not
:
//very useful example
true.not().or(true).and(false).xor(true)