Skip to content

Instantly share code, notes, and snippets.

@Ephraimiyanda
Created February 16, 2026 15:37
Show Gist options
  • Select an option

  • Save Ephraimiyanda/1df45efe9bfef889263588a18fb79cde to your computer and use it in GitHub Desktop.

Select an option

Save Ephraimiyanda/1df45efe9bfef889263588a18fb79cde to your computer and use it in GitHub Desktop.
Number of 1 Bits

Question

Approach

  1. The number is converted to binary then split into an array.
  2. The array is the filtered to only the hold the 1's .
  3. The length of the array is returned

Complexity

  • Time complexity:
  • Space complexity:

Code

function hammingWeight(n: number): number {
    return n.toString(2).split("").filter((num) => num === "1").length
};
scrnli_cVxQLOq4wC0SHF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment