Leetcode - 136. Single Number
Leetcode - 136. Single Number
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Solution
1
2
3
4
5
6
7
8
func singleNumber(nums []int) int {
nonRepeatingNum := 0
for _, num := range nums{
// perform XOR operation that will emmit the number occuring double
nonRepeatingNum^=num
}
return nonRepeatingNum
}
This post is licensed under CC BY 4.0 by the author.