Solving Single Number II in go. Please try yourself first to solve the problem and submit your implementation to LeetCode before looking into solution.

Problem Description

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Example 1:

Input: [2,2,3,2]
Output: 3

Example 2:

Input: [0,1,0,1,0,1,99]
Output: 99

func singleNumberIII(nums []int) int {
    na, nb, nc := 0, 0, 0
    for i := 0; i < len(nums); i++ {
        nb = nb ^ (nums[i] & na)
        na = (na ^ nums[i]) & ^nc
        nc = nc ^ (nums[i] & ^na & ^nb)
    }
    return na & ^nb & ^nc
}

func singleNumberIIII(nums []int) int {
    twos, threes, ones := 0xffffffff, 0xffffffff, 0
    for i := 0; i < len(nums); i++ {
        threes = threes ^ (nums[i] & twos)
        twos = (twos ^ nums[i]) & ^ones
        ones = ones ^ (nums[i] & ^twos & ^threes)
    }
    return ones
}

See the full details of the problem Single Number II at LeetCode

Originally posted at: @github.com/halfrost/LeetCode-Go