Logo
Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Solution: Single Number

Problem Statement

In a non-empty array of integers, every number appears twice except for one, find that single number.

Example 1:

Input: 1, 4, 2, 1, 3, 2, 3
Output: 4

Example 2:

Input: 7, 9, 7
Output: 9

Constraints:

  • 1 <= nums.length <= 3 * 10<sup>4</sup>
  • -3 * 10<sup>4</sup> <= nums[i] <= 3 * 10<sup>4</sup>
  • Each element in the array appears twice except for one element which appears only once.

Solution

One straight forward solution can be to use a HashMap kind of data structure and iterate through the input:

1

.....

.....

.....

Like the course? Get enrolled and start learning!