Swift solution


  • 0
    class Solution {
        func totalHammingDistance(_ nums: [Int]) -> Int {
            let n = nums.count
            var result = 0
            
            for i in 0..<32 {
                var bitCount = 0
                for j in 0..<n {
                    bitCount += (nums[j] >> i) & 1
                }
                result += bitCount * (n - bitCount)
            }
            
            return result
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.