-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathhammingDistance.js
More file actions
37 lines (28 loc) · 870 Bytes
/
hammingDistance.js
File metadata and controls
37 lines (28 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
The [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) between two integers is the number of positions at which the corresponding bits are different.
Given two integers `x` and `y`, calculate the Hamming distance.
Input: x = 1, y = 4
Output: 2
Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
The above arrows point to positions where the corresponding bits are different.
*/
/**
*
* @param {number} x
* @param {number} y
* @returns {number}
*/
const hammingDistance = (x, y) => {
/*
input -> 1, 4
1. XOR of two numbers -> 1 ^ 4
2. Convert the number to its binary form -> 101
3. Remove 0s from the string and return the length of this new string
*/
return (x ^ y).toString(2).replace(/0/g, "").length;
};
hammingDistance(1, 4); // 2
hammingDistance(2, 5); // 3