Coding Challenge Practice - Question 77

Published: (December 13, 2025 at 05:45 PM EST)
1 min read
Source: Dev.to

Source: Dev.to

Problem Description

Implement a function that finds the integer that appears only once in an array where every other integer appears exactly twice.

Solution Explanation

The XOR operation (^) has a useful property:

  • a ^ a = 0 – two identical numbers cancel each other out.
  • 0 ^ b = b – XOR with zero leaves the other number unchanged.

By XOR‑ing all elements of the array, every pair of identical numbers reduces to 0, leaving only the single, unpaired integer.

Code

function findSingle(arr) {
  let result = 0;

  for (let num of arr) {
    result ^= num;
  }

  return result;
}
Back to Blog

Related posts

Read more »

Monkey Market

Part 1 Another math gauntlet I get to program a bunch of math operations. Some will be part of several conditionals. I've done it before. I'm confident I can d...

Coding Challenge Practice - Question 69

Problem Description Create a function that returns the n-th term of the “look‑and‑say” sequence as a string. The sequence starts with '1' for n = 1. Each sub...