Find the Single Number in an Array

Find the Single Number in an Array

In a given array of integers, every element appears twice except for one number.

Your task is to identify the integer that appears only once, using an efficient algorithm that operates in linear time and constant space.

Pair with given sum - Two Sum

Problem Overview

You are provided with a non-empty array called nums, where each integer appears exactly twice, except for one unique number that appears only once. The challenge is to find that lone number with the following constraints:

  • Time Complexity: O(n)
  • Space Complexity: O(1)

The array can contain up to 10,000 elements, and the values within the array range from -10,000 to 10,000.

Example Scenarios

Let’s look at two examples to better understand the problem:

In this case, the number 3 appears twice, and the number 2 appears only once.

Here, 7 and 6 appear twice, but 8 appears only once.

Problem Constraints:

  • The length of the array is between 1 and 10,000.
  • The values in the array range from -10,000 to 10,000.

Given these constraints, your solution must be efficient both in terms of time and space.

Efficient Solution: XOR Approach

The key to solving this problem efficiently is using the XOR operation. XOR has a unique property that makes it particularly useful in this scenario:

  • x ^ x = 0 (Any number XORed with itself results in 0).
  • x ^ 0 = x (Any number XORed with 0 results in the number itself).
  • XOR is both commutative and associative, meaning the order of operations doesn’t matter.

Thus, when you XOR all the numbers in the array, the numbers that appear twice will cancel each other out, leaving only the number that appears once.

 

Pair with Given Sum - 2Sum Solution

Recommendation – You should aim for a solution with O(n) time and O(n) space, where n is the size of the input array.

Hints for solving problems

Hint 1 :

A brute force solution would be to check every pair of numbers in the array. This would be an O(n^2) solution. Can you think of a better way? Maybe in terms of mathematical equation?

Hint 2:

Given, We need to find indices i and j such that i != j and nums[i] + nums[j] == target. Can you rearrange the equation and try to fix any index to iterate on?

Hint 3:

Iterate through the array while checking if target – nums[i] exists in the hash map. If not, store the current element with its index in the hash map and continue. This ensures O(1) lookups.

There are mainly 4 approach to solve this problem – 

  1. Brute Force Method
  2. Sorting Method
  3. Hash Map(Two Pass) Method
  4. Hash Map(One Pass) Method

1. Brute Force Method

This method check all possible pairs of numbers in the array to find the ones that add up to the target. Simple but slow for large arrays.

  • Time complexity: O(n^2)
  • Space complexity: O(1)

Code

2. Sorting Method

This method sort the array, then use two pointers (one at the start and one at the end) to find the pair that sums to the target.

  • Time complexity: O(nlogn)
  • Space complexity: O(n)

Code

3. Hash Set

This method first, store all numbers and their indices in a hash map. Then, iterate again to find if the complement (target – current number) exists in the map.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

4. Bit Manipulation

Bit manipulation refers to the process of directly operating on the individual bits of a number. In binary, all data is represented as a series of bits (0s and 1s).

  • Time & Space Complexity

    • Time complexity: O(n)
    • Space complexity: O(1)

Code

More Articles