Single Number in TypeScript with Bit Manipulation

Hero image for Single Number in TypeScript with Bit Manipulation. Image by Sean Fahrenbruch.
Hero image for 'Single Number in TypeScript with Bit Manipulation.' Image by Sean Fahrenbruch.

On the surface, bit manipulation can look a little daunting, more mysterious than it really is. The code notation itself is compact, the operators are easy to misuse, and many explanations jump straight from the problem statement to a cleverlooking line of code without explaining why or how it works. That is exactly why the Single Number problem is worth slowing down for.

The problem is simple on the surface; every value in an array appears twice except for one. Our solution needs to return the value that appears once. There are any number of different ways to approach this one; we could solve that with a hash map, for example, but the really interesting approach uses XOR and constant extra space.

When viewed like this, it stops feeling like a trick and starts feeling like a very tidy use of the problem constraints.


Why Xor is the Right Tool Here

The XOR operator has two properties that matter most for this problem:

  • First, a value XOR'd with itself becomes zero.
  • Second, zero XOR'd with a value gives that value back.

That means matching pairs cancel one another out, leaving only the unique number behind.

It is an algorithm idea that feels almost algebraic. We are not tracking counts explicitly; we are relying on the structure of the operation itself to find and remove matching pairs of numbers. There is a nice echo there of the more mathematical side of computing, even if JavaScript remains far removed from the purer functional traditions of Haskell or Lisp.


A Practical TypeScript Solution

export const singleNumber = (values: number[]): number => {  let result = 0;  for (const value of values) {    result ^= value;  }  return result;};

The loop is doing one job only. It accumulates the XOR of every value. Each duplicate pair cancels itself out on the way through, so by the end, the accumulator contains only the unmatched number.


Why This Beats a Hash Map

A hash map solution is perfectly readable, and in many production problems, it would be a sensible first pass. Here, though, the problem constraints are pointing us towards something stronger. We know every nonunique value appears exactly twice, which means we can turn the whole task into a cancellation problem instead of a counting problem.

That saves space and expresses the core logic more directly. It also gives us a clearer complexity story: linear time and constant extra space.

It is easy to think that bit manipulation solutions are automatically less readable. Sometimes they are, but not when the underlying rule is this tight. If we explain the XOR properties clearly, the final implementation is arguably much simpler than the countbased alternative.

It is worth bearing in mind that in this specific LeetCode task, this is a great solution. However, another mistake you could easily make it trying to apply this pattern where the constraints do not match. For example, if some numbers can appear three times, or if more than one unique value exists, the XORonly approach no longer answers the same question. Clever code is only correct when the data contract supports it.


Why the Explanation Matters Here

As a function, it is easy to test because the behaviour is deterministic and the boundary is small. Maintainability comes from making sure that we are using clear naming, labelling the intent clearly, and documenting why XOR works, because that explanation is what stops the implementation from looking cryptic later. Scalability matters less here, but the algorithm still scales well in time and memory terms because it does not need extra structures as the input grows.

For the original problem statement and the lowerlevel details behind the solution, these references are worth a look:


Wrapping up

The Single Number problem is a good reminder that small algorithmic problems often reward careful reading more than heavylifting. Once you've gotten your head around the XOR properties, the solution becomes almost surprisingly direct.

Key Takeaways

  • XOR works because duplicate values cancel each other out.
    The bitwise solution uses linear time and constant extra space.
    The approach is only valid because the input guarantees exactly one nonduplicated value.

Single Number is a good reminder that elegant algorithms are often just careful readings of the constraints. Notice what the data is promising us, and the implementation becomes much smaller and much more satisfying.


Categories:

  1. Development
  2. Guides
  3. JavaScript
  4. LeetCode