Leetcode Hamming Distance problem solution YASH PAL, 31 July 2024 In this Leetcode Hamming Distance problem solution The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, return the Hamming distance between them. Problem solution in Python. class Solution: def hammingDistance(self, x: int, y: int) -> int: h_dist = 0 while x or y: if (x & 1) != (y & 1): h_dist += 1 x = x >> 1 y = y >> 1 return h_dist Problem solution in Java. class Solution { public int hammingDistance(int x, int y) { String xout="",yout=""; int binaryx[] = new int[31],binaryy[] = new int[31]; int cntx=0,cnty=0,count=0; while(x>0){ binaryx[cntx++]=x%2; x/=2; } while(y>0){ binaryy[cnty++]=y%2; y/=2; } for(int i=0;i<31;i++){ if(binaryx[i]!=binaryy[i]) count++; } return count; } } Problem solution in C++. class Solution { public: int hammingDistance(int x, int y) { int z = x xor y; int ans = 0; while (z){ z -= z & (-z); ans ++; } return ans; } }; Problem solution in C. int hammingDistance(int x, int y) { int count = 0; while(x != 0 || y != 0) { if( (x&1) != (y&1)) count++; x >>= 1; y >>= 1; } return count; } coding problems