Skip to content

Commit

Permalink
bit manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Azureki committed May 4, 2019
1 parent fa77a98 commit 30a57c1
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package problem287

func findDuplicate(nums []int) int {
res := 0
for i := uint(0); i < 32; i++ {
countNormal, countDup := 0, 0
b := 1 << i
for j := 0; j < len(nums); j++ {
if j&b > 0 {
countNormal++
}
if nums[j]&b > 0 {
countDup++
}
}
if countDup > countNormal {
res += b
}
}
return res
}

0 comments on commit 30a57c1

Please sign in to comment.