Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
1.
Introduction
2.
Problem Statement
3.
Explanation of Problem Statement:
3.1.
Constraint :
4.
Solution:
4.1.
1. Sorting Technique:
4.1.1.
Algorithm for Sorting Technique:
4.1.2.
Code for Sorting Technique:
4.2.
2. HashMap Technique:
4.2.1.
Algorithm for HashMap Technique:
4.2.2.
Code for HashMap Technique:
5.
6.
Key Takeaway
Last Updated: Mar 27, 2024

# Single Number III

## Introduction

Today’s problem, i.e., –Single Number III,” is highly asked in product-based companies like Amazon, Google, and Microsoft.

We’ll go over all the methods, including brute force and the most efficient way to solve this problem.

we are going to solve one of the problems of Leetcode from by different approach.

## Problem Statement

Given an integer array, 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:

Example 2:

## Explanation of Problem Statement:

In this problem, an array list is given which contains the element, which there are only two elements that come for only one time, the other element comes more than two times, then we have to return the two elements which come single times in array List.

E.g., if nums=[1,2,1,3,2,5] here in the nums array list, each element comes more than one time except 3 and 5, so we have to return these two elements. The order of returning the element does not matter, as stated in the problem statement.

Output : [3,5] -> comes only one time in the array, so it is our answer.

### Constraint :

1. Only Two elements in the List are coming single times, and others are repeated more than twice.
2. When the size of the array list is even, i.e., say N(6), then for the N-2(4) element, the N-2/2 element gets repeated at least two times, and the rest two elements come for a single time.
E.g :  [ 1, 2, 1, 2, 3, 4]  here N=6 and N-2=4/2=2 element comes at least 2 times 1 and 2 , rest element
3. When the size of the array list is odd, i.e., say N(5), then at least one element comes more than two times to maintain Constraint 1. Of the question, the rest two-element comes from single times.
4. Above points 2 and 3 is a general understanding of the size of the array for N=6 and 5.

## Solution:

### 1. Sorting Technique:

In this approach, we came up with a brute-force type approach to sort the element and iterate over the array’s length and check the previous and next element. If it matches that element, it is repeated, and we move forward to the next element. If the previous and next of the present element are not found to be the same, then it’s the singly occurring element in the list as an array is already sorted.

#### Algorithm for Sorting Technique:

1. Declare the size and  Array of size say “N”  and enter “N” element in it.
2. Element is entered following the Constraint of the question as only two elements are entered one time, rest are entered at least two times.
3. Sort the Array in ascending order or descending order as per your choice.
4. Now check the corner case at index “0” and “N-1” in separate conditions with the next and previous elements, respectively.
5. Now run a loop from index =1 to N-2, with a conditional statement of checking the i+1th  and i-1th position element with present ith position element to verify more than one occurrence in the array list.
6. If a single occurrence is found, declare a size length of 2 Array as constraint tells only two elements to occur a single time in the array list.
7. At last completion of loop iteration over 1 to N-2, covering the lower and upper corner case of 1st and the previous element returns a temporary declare array containing single element occurrence in Array List.

#### Code for Sorting Technique:

Input:

Output:

Time Complexity: O(N*log N), N* logN for sort array, N for iteration and comparison last, following, with the present element.

Space Complexity: O(1), as we are declaring an extra array of size two which constant time

### 2. HashMap Technique:

In this approach, we try to optimize the Time complexity from O(N*log(N)) to O(N) but to do so, our space complexity gets suffer as previous its O(constant), i.e., O(1) but using HashMap it becomes O(N) itself as for each element we are maintaining the frequency of every element.

#### Algorithm for HashMap Technique:

1. Declare the size and  Array of size say “N”  and enter “N” element in it.
2. Element is entered following the Constraint of the question as only two elements are entered one time, rest is entered at least two times.
3. Now declare a HashMap, which contains the frequency of each of the elements present in the Array.
4. Now iterate over the HashMap and check the KeySet value whose value is one. Then, store that element value in the array by overwriting it, so no extra space is created as already HashMap has taken N extra space.
5. After iteration over all the elements, assign the singly occurrence element array temporary declared of O(2) size, which usually takes O(1) Space in Constant Time.
6. At last, return the array.

#### Code for HashMap Technique:

Input:

Output:

Time Complexity: Time Complexity of the HashMap Technique is O(N).

Space Complexity: O(N) for HashMap and O(constant) for temporary arrays, which is generally O(N+constant) hence O(N).
Also see, Euclid GCD Algorithm

## Frequently Asked Question:

1. What is Sorting?
Process of Arranging the element in Ascending or descending or specific order.
2. What is Time Complexity for sort the element?
There are different types of sorting AlgorithmsSorting. Each has an additional TC.
The list of some Algorithm is as follows

Source: Sorting algorithms

1. Which inbuilt Sorting Function uses a sorting Algorithm?
In Java, it is Quick Sort, with TC of O(N*log(N)).
2. What is the Time Complexity of Sorting Technique of Single Number III?
Time Complexity is O(N*log(N)), and Space Complexity is O(1).

3. What is the Time Complexity of the HashMap Technique of Single Number III?
Time Complexity is O(N), and Space Complexity is O(N).

Also Read - Strong number in c

## Key Takeaway

In this blog, we generally talked about the different approaches; one is Sorting. Another is HashMap to deal with the Two Singly element occur in Array list and try to do the solution in brute force manner sorting technique in O(N*log(N)) time complexity to optimize the solution in O(N) using HashMap.

If you feel confident, then why don’t you give it a try by submitting it here? Single Number III.

If you are a beginner in coding and want to learn DSA, you can look for our guided path for DSA, which is free!

Live masterclass