Single Number 3 LeetCode Solution - The Coding Shala

Home >> LeetCode >> Single Number 3

 In this post, we will learn how to solve LeetCode's Single Number 3 problem and will implement its solution in Java.

Single Number 3 Problem

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.

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]

Practice this problem on LeetCode.

LeetCode - Single Number 3 Java Solution

Approach 1

Using HashMap [ Extra Space].

Java Program: 

class Solution {
    public int[] singleNumber(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        int[] ans = new int[2];
        for(int num : nums) {
            if(map.containsKey(num)) map.put(num, map.get(num)+1);
            else map.put(num, 1);
        }
        ArrayList<Integer> arr = new ArrayList<>();
        for(int num : nums) {
            if(map.get(num) == 1) arr.add(num);
        }
        ans[0] = arr.get(0);
        ans[1] = arr.get(1);
        return ans;
    }
}

Approach 2

Using Bit Manipulation.

We will follow the below steps:

  • step 1: XOR all the numbers, the result will be res1 ^ res2.
  • step 2: Now our result contains both single numbers xor if a bit is zero in xor that means both the bit in res1 and res 2 is the same so we can't determine with that. We need to find the 1 bit index. Traverse all the 32-bit indexes of the XOR result, once we find there exists 1 bit on the index, break the loop.
  • Step 3: Now we have an index that has 1 bit so one of our numbers has 1 bit at that index so we will traverse all the numbers in the input array, if we find a number with a bit index that is not 0 then we can use res1 XOR current num to iteratively fill out an effective bit in res1.For example res1 ^ n1 ^ n1 = res1. [n1 appears twice]
  • Step 4: we have res1 so by doing res1 ^ allnNumberoXor we will get res2.
Java Program: 

class Solution {
    public int[] singleNumber(int[] nums) {
        int allNumberXor = 0;
        //single1 ^ single2
        for(int num : nums) {
            allNumberXor ^= num;
        }
        
        //find ith bit as 1;
        int bitIndex;
        for(bitIndex = 0; bitIndex < 32; bitIndex++) {
            if((allNumberXor & (1 << bitIndex)) != 0) break;
        }
        
        //check this bitIndex for every number to find single1
        int single1 = 0;
        int single2 = 0;
        for(int num : nums) {
            if((num & (1 << bitIndex)) !=0) {
                single1 ^= num;
            }
        }
        
        //get single2
        single2 = single1 ^ allNumberXor;
        
        return new int[] {single1, single2};
    }
}


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala