This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 4: Optimisation
XOR cancels pairs, n & (n - 1) drops the lowest set bit.
Builds on Hash Maps & Sets.
The question to ask
Would looking at the numbers as bits make pairs cancel or the counting trivial?
A handful of identities cover most bit questions. XOR of a number with itself is 0 and with 0 is itself, so XOR-ing everything cancels every pair and leaves the odd one out. n & (n - 1) clears the lowest set bit, so looping it counts bits. Shifting and masking reads or sets bit i. Reach for these when the question says "without extra space", "appears twice except one", "binary representation", or forbids the + operator.
Time O(n), or O(number of bits) per number · Space O(1)
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
n & (n - 1) clears the lowest 1 bit, so the loop runs once per set bit rather than once per bit position.
function hammingWeight(n: number): number {
let count = 0;
while (n !== 0) {
n &= n - 1;
count++;
}
return count;
}i >> 1 drops the last bit of i, so bits[i] = bits[i >> 1] + (i & 1). A one-line DP over the bits.
function countBits(n: number): number[] {
const bits = new Array<number>(n + 1).fill(0);
for (let i = 1; i <= n; i++) bits[i] = bits[i >> 1] + (i & 1);
return bits;
}XOR adds each bit without the carry; AND then shift left gives the carry. Repeat until there's no carry. JavaScript bitwise operators work on 32-bit integers, so negatives work too.
function getSum(a: number, b: number): number {
while (b !== 0) {
const carry = (a & b) << 1;
a ^= b;
b = carry;
}
return a;
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Every element appears twice except for one, which appears exactly once. Find it in linear time with constant extra space.
"Appears twice" except one, plus "constant extra space": XOR everything.
Not attempted yet
Return the number of set bits (1 bits) in the binary representation of a positive integer.
Count "1 bits": n & (n - 1) drops one per loop.
Not attempted yet
For every i from 0 to n, return the number of 1 bits in the binary representation of i.
"1 bits" for every i: bits[i] = bits[i >> 1] + (i & 1) reuses the smaller answer.
Not attempted yet
Reverse the bits of a 32-bit unsigned integer.
"Reverse bits" of a "32-bit" value: shift the lowest bit of n into the result 32 times, then >>> 0.
Not attempted yet
Given n distinct numbers from the range 0 to n, return the only number missing from the range.
The "missing" one: XOR every index and value, or subtract the sum from n(n + 1) / 2.
Not attempted yet
Return the sum of two integers without using the operators + and -.
"Without using the operators": XOR for the sum without carry, AND and shift for the carry, repeat.
Not attempted yet
Reverse the digits of a signed 32-bit integer, returning 0 if the result overflows the 32-bit range.
Digits rather than bits, but it's about "32-bit" overflow: check the bounds before each multiply by 10.
Not attempted yet
Every element appears three times except for one, which appears exactly once. Find it with constant extra space.
"Appears three times": count each bit position across all numbers modulo 3; the leftover bits are the answer.
Not attempted yet