Skip to content

Latest commit

 

History

History

single-number

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Single Number

Link to LeetCode page

Difficulty: Easy

Topics: Hash table, bit manipulation.

Solution Explanation

This solution uses the bitwise XOR operator to return the unique value in the array.

To illustrate the bitwise XOR operation using the given Example 2 from LeetCode page:

Input: [4,1,2,1,2]

Bitwise XOR operation with 0 on the elements:
0 ^ 4 ^ 1 ^ 2 ^ 1 ^ 2
> 4

We want to go through every element in the array, perform the bitwise XOR operation, and return a single value output. Instead of the conventional for loop, we use array's reduce() function to achieve this, which is more succinct.

Complexity Analysis

Time complexity: O(n) because we go through every elements in the array once.

Space complexity: O(1) because the memory used is always the same regardless of the number of elements.