- The number is converted to binary then split into an array.
- The array is the filtered to only the hold the
1's. - The length of the array is returned
- Time complexity:
- Space complexity:
function hammingWeight(n: number): number {
return n.toString(2).split("").filter((num) => num === "1").length
};