Skip to content

Commit c2a7d93

Browse files
committed
leetcode
1 parent 481bcc3 commit c2a7d93

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

SingleNumber/single_number.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,34 @@
11
package main
2+
3+
import "sort"
4+
5+
func singleNumber(nums []int) int {
6+
var ones int = 0
7+
var twos int = 0
8+
9+
for i := 0; i < len(nums); i++ {
10+
var number int = nums[i]
11+
ones ^= (number & ^twos)
12+
twos ^= (number & ^ones)
13+
}
14+
15+
return ones
16+
}
17+
18+
func anotherSingle(nums []int) int {
19+
var index int = len(nums) - 1
20+
sort.Ints(nums)
21+
var value int = 0
22+
for value < index {
23+
if nums[value] != nums[value+1] {
24+
return nums[value]
25+
} else {
26+
27+
value += 2
28+
}
29+
if index%2 == 1 {
30+
return nums[index]
31+
}
32+
}
33+
return -1
34+
}

0 commit comments

Comments
 (0)