Convert the number to binary and while doing so keep counting the 1s
func hammingWeight(num uint32) int {
total := 0
for num > 0 {
if num % 2 == 1 {
total++
}
num = num / 2
}
return total
}
Solution in Go
Convert the number to binary and while doing so keep counting the 1s
func hammingWeight(num uint32) int {
total := 0
for num > 0 {
if num % 2 == 1 {
total++
}
num = num / 2
}
return total
}
Get notified once/twice per month when new articles are published.