The Indian Engineer

Problem 260 Single Number III

Posted on 5 mins

Array Bit-Manipulation

Problem Statement

Link - Problem 260

Question

Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.

You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.

Example 1:

Input: nums = [1,2,1,3,2,5]
Output: [3,5]
Explanation:  [5, 3] is also a valid answer.

Example 2:

Input: nums = [-1,0]
Output: [-1,0]

Example 3:

Input: nums = [0,1]
Output: [1,0]

Constraints:

Solution

class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        unordered_map<int, int> frequencyMap;


        for (int num : nums) {
            frequencyMap[num]++;
        }


        vector<int> result;
        for (const auto& entry : frequencyMap) {
            if (entry.second == 1) {
                result.push_back(entry.first);
            }
        }

        return result;
    }
};

Complexity Analysis

| Algorithm  | Time Complexity | Space Complexity |
| ---------- | --------------- | ---------------- |
| Hash Table | O(n)            | O(n)             |

Explanation

Intial Thoughts

To solve this problem, we can start by thinking about how to identify the two unique numbers in the array. One approach is to use a frequency map, where we count the occurrences of each number. We can also think about using bitwise operations to find the unique numbers. Another idea is to use a divide-and-conquer approach, where we split the array into smaller sub-arrays and find the unique numbers in each sub-array. Additionally, we need to consider the constraints of linear runtime complexity and constant extra space.

Intuitive Analysis

An intuitive solution involves using a XOR operation, where we XOR all the numbers in the array. The result will be the XOR of the two unique numbers. We can then find the rightmost set bit in the result, which will help us to divide the numbers into two groups. By XORing the numbers in each group separately, we can find the two unique numbers. This approach works because XOR of all elements gives us the XOR of the two unique elements. Another way to look at it is to think of the XOR operation as a way to ‘cancel out’ the numbers that appear twice, leaving us with the two unique numbers.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.


Similar Questions:

Title URL Difficulty
Single Number https://leetcode.com/problems/single-number Easy
Single Number II https://leetcode.com/problems/single-number-ii Medium
Find The Original Array of Prefix Xor https://leetcode.com/problems/find-the-original-array-of-prefix-xor Medium
Find the XOR of Numbers Which Appear Twice https://leetcode.com/problems/find-the-xor-of-numbers-which-appear-twice Easy