# Find the number that appears once, and the other numbers twice

Problem Statement: Given a non-empty array of integers arr, every element appears twice except for one. Find that single one.

Examples
```Example 1:
Input Format: arr[] = {2,2,1}
Result: 1
Explanation: In this array, only the element 1 appear once and so it is the answer.

Example 2:
Input Format: arr[] = {4,1,2,1,2}
Result: 4
Explanation: In this array, only element 4 appear once and the other elements appear twice. So, 4 is the answer.
```
Practice:

Disclaimer: Don’t jump directly to the solution, try it out yourself first.

Brute Force Approach Better Approach 1 Better Approach 2 Optimal Approach
Brute Force Approach
Algorithm / Intuition

### Intuition:

For every element present in the array, we will do a linear search and count the occurrence. If for any element, the occurrence is 1, we will return it.

### Approach:

The steps are as follows:

1. First, we will run a loop(say i) to select the elements of the array one by one.
2. For every element arr[i], we will perform a linear search using another loop and count its occurrence.
3. If for any element the occurrence is 1, we will return it.
Code
``````

#include <bits/stdc++.h>
using namespace std;

int getSingleElement(vector<int> &arr) {
// Size of the array:
int n = arr.size();

//Run a loop for selecting elements:
for (int i = 0; i < n; i++) {
int num = arr[i]; // selected element
int cnt = 0;

//find the occurrence using linear search:
for (int j = 0; j < n; j++) {
if (arr[j] == num)
cnt++;
}

// if the occurrence is 1 return ans:
if (cnt == 1) return num;
}

//This line will never execute
//if the array contains a single element.
return -1;
}

int main()
{
vector<int> arr = {4, 1, 2, 1, 2};
int ans = getSingleElement(arr);
cout << "The single element is: " << ans << endl;
return 0;
}
```
```
``````

import java.util.*;

public class tUf {
public static int getSingleElement(int []arr) {
// Size of the array:
int n = arr.length;

//Run a loop for selecting elements:
for (int i = 0; i < n; i++) {
int num = arr[i]; // selected element
int cnt = 0;

//find the occurrence using linear search:
for (int j = 0; j < n; j++) {
if (arr[j] == num)
cnt++;
}

// if the occurrence is 1 return ans:
if (cnt == 1) return num;
}

//This line will never execute
//if the array contains a single element.
return -1;
}

public static void main(String args[]) {
int[] arr = {4, 1, 2, 1, 2};
int ans = getSingleElement(arr);
System.out.println("The single element is: " + ans);

}
}
```
```
``````

def getSingleElement(arr):
# Size of the array:
n = len(arr)

# Run a loop for selecting elements:
for i in range(n):
num = arr[i]  # selected element
cnt = 0

# Find the occurrence using linear search:
for j in range(n):
if arr[j] == num:
cnt += 1

# If the occurrence is 1, return the number:
if cnt == 1:
return num

# This line will never execute
# if the array contains a single element.
return -1

def main():
arr = [4, 1, 2, 1, 2]
ans = getSingleElement(arr)
print("The single element is:", ans)

if __name__ == '__main__':
main()

```
```
``````

function getSingleElement(arr) {
// Size of the array:
const n = arr.length;

// Run a loop for selecting elements:
for (let i = 0; i < n; i++) {
const num = arr[i]; // selected element
let cnt = 0;

// Find the occurrence using linear search:
for (let j = 0; j < n; j++) {
if (arr[j] === num) {
cnt++;
}
}

// If the occurrence is 1, return the number:
if (cnt === 1) {
return num;
}
}

// This line will never execute
// if the array contains a single element.
return -1;
}

function main() {
const arr = [4, 1, 2, 1, 2];
const ans = getSingleElement(arr);
console.log("The single element is:", ans);
}

main();

```
```

Output: The single element is: 4

Complexity Analysis

Time Complexity: O(N2), where N = size of the given array.
Reason: For every element, we are performing a linear search to count its occurrence. The linear search takes O(N) time complexity. And there are N elements in the array. So, the total time complexity will be O(N2).

Space Complexity: O(1) as we are not using any extra space.

Better Approach 1
Algorithm / Intuition

### Intuition:

In the previous approach, we were finding the occurrence of an element using linear search. We can optimize this using hashing technique. We can simply hash the elements along with their occurrences in the form of (key, value) pair. Thus, we can reduce the cost of finding the occurrence and hence the time complexity.

Now, hashing can be done in two different ways and they are the following:

• Array hashing(not applicable if the array contains negatives or very large numbers)
• Hashing using the map data structure

### Array hashing:

In order to hash using an array, we need to first find the maximum element(say maxElement) to get the range of the hash array. The index of the hash array will represent the elements of the given array and the value stored in that index will be the number of occurrences. Now, the size of the array must be maxElement+1 as we need to hash the maxElement itself.

Approach:

The steps are as follows:

1. First, we will find the maximum element(say maxElement) to know the size of the hash array.
2. Then we will declare a hash array of size maxElement+1.
3. Now, using another loop we will store the elements of the array along with their frequency in the hash array. (i.e. hash[arr[i]]++)
4. After that, using another loop we will iterate over the hash array, and try to find the element for which the frequency is 1, and finally, we will return that particular element.

Note: While searching for the answer finally, we can either use a loop from 0 to n(i.e. Size of the array) or use a loop from 0 to maxElement. So, the time complexity will change accordingly.

Now, using array hashing it is difficult to hash the elements of the array if it contains negative numbers or a very large number. So to avoid these problems, we will be using the map data structure to hash the array elements.

Code
``````

#include <bits/stdc++.h>
using namespace std;

int getSingleElement(vector<int> &arr) {

//size of the array:
int n = arr.size();

// Find the maximum element:
int maxi = arr[0];
for (int i = 0; i < n; i++) {
maxi = max(maxi, arr[i]);
}

// Declare hash array of size maxi+1
// And hash the given array:
vector<int> hash(maxi + 1, 0);
for (int i = 0; i < n; i++) {
hash[arr[i]]++;
}

//Find the single element and return the answer:
for (int i = 0; i < n; i++) {
if (hash[arr[i]] == 1)
return arr[i];
}

//This line will never execute
//if the array contains a single element.
return -1;
}

int main()
{
vector<int> arr = {4, 1, 2, 1, 2};
int ans = getSingleElement(arr);
cout << "The single element is: " << ans << endl;
return 0;
}
```
```
``````

import java.util.*;

public class tUf {
public static int getSingleElement(int []arr) {
//size of the array:
int n = arr.length;

// Find the maximum element:
int maxi = arr[0];
for (int i = 0; i < n; i++) {
maxi = Math.max(maxi, arr[i]);
}

// Declare hash array of size maxi+1
// And hash the given array:
int[] hash = new int[maxi + 1];
for (int i = 0; i < n; i++) {
hash[arr[i]]++;
}

//Find the single element and return the answer:
for (int i = 0; i < n; i++) {
if (hash[arr[i]] == 1)
return arr[i];
}

//This line will never execute
//if the array contains a single element.
return -1;
}

public static void main(String args[]) {
int[] arr = {4, 1, 2, 1, 2};
int ans = getSingleElement(arr);
System.out.println("The single element is: " + ans);

}
}
```
```
``````

def getSingleElement(arr):
# Size of the array:
n = len(arr)

# Find the maximum element:
maxi = max(arr)

# Declare hash array of size maxi+1
# And hash the given array:
hash = [0] * (maxi + 1)
for num in arr:
hash[num] += 1

# Find the single element and return the answer:
for num in arr:
if hash[num] == 1:
return num

# This line will never execute
# if the array contains a single element.
return -1

def main():
arr = [4, 1, 2, 1, 2]
ans = getSingleElement(arr)
print("The single element is:", ans)

if __name__ == '__main__':
main()

```
```
``````

function getSingleElement(arr) {
// Size of the array:
let n = arr.length;

// Find the maximum element:
let maxi = arr[0];
for (let i = 0; i < n; i++) {
maxi = Math.max(maxi, arr[i]);
}

// Declare hash array of size maxi+1
// And hash the given array:
let hash = new Array(maxi + 1).fill(0);
for (let i = 0; i < n; i++) {
hash[arr[i]]++;
}

// Find the single element and return the answer:
for (let i = 0; i < n; i++) {
if (hash[arr[i]] === 1) {
return arr[i];
}
}

// This line will never execute
// if the array contains a single element.
return -1;
}

function main() {
let arr = [4, 1, 2, 1, 2];
let ans = getSingleElement(arr);
console.log("The single element is:", ans);
}

main();

```
```

Output: The single element is: 4

Complexity Analysis

Time Complexity: O(N)+O(N)+O(N), where N = size of the array
Reason: One O(N) is for finding the maximum, the second one is to hash the elements and the third one is to search the single element in the array.

Space Complexity: O(maxElement+1) where maxElement = the maximum element of the array.

Better Approach 2
Algorithm / Intuition

### Intuition:

The intuition will be the same as the array hashing. The only difference here is we will use the map data structure for hashing instead of using another array for hashing.

### Approach:

The steps are as follows:

1. First, we will declare a map.
2. Now, using a loop we will store the elements of the array along with their frequency in the map data structure.
3. Using another loop we will iterate over the map, and try to find the element for which the frequency is 1, and finally, we will return that particular element.
Code
``````

#include <bits/stdc++.h>
using namespace std;

int getSingleElement(vector<int> &arr) {

//size of the array:
int n = arr.size();

// Declare the hashmap.
// And hash the given array:
map<int, int> mpp;
for (int i = 0; i < n; i++) {
mpp[arr[i]]++;
}

//Find the single element and return the answer:
for (auto it : mpp) {
if (it.second == 1)
return it.first;
}

//This line will never execute
//if the array contains a single element.
return -1;
}

int main()
{
vector<int> arr = {4, 1, 2, 1, 2};
int ans = getSingleElement(arr);
cout << "The single element is: " << ans << endl;
return 0;
}
```
```
``````

import java.util.*;

public class tUf {
public static int getSingleElement(int []arr) {
//size of the array:
int n = arr.length;

// Declare the hashmap.
// And hash the given array:
HashMap<Integer, Integer> mpp = new HashMap<>();
for (int i = 0; i < n; i++) {
int value = mpp.getOrDefault(arr[i], 0);
mpp.put(arr[i], value + 1);
}

//Find the single element and return the answer:
for (Map.Entry<Integer, Integer> it : mpp.entrySet()) {
if (it.getValue() == 1) {
return it.getKey();
}
}

//This line will never execute
//if the array contains a single element.
return -1;
}

public static void main(String args[]) {
int[] arr = {4, 1, 2, 1, 2};
int ans = getSingleElement(arr);
System.out.println("The single element is: " + ans);

}
}
```
```
``````

def get_single_element(arr):
# Size of the array:
n = len(arr)

# Declare the hashmap.
# And hash the given array:
hashmap = {}
for num in arr:
hashmap[num] = hashmap.get(num, 0) + 1

# Find the single element and return the answer:
for num, count in hashmap.items():
if count == 1:
return num

# This line will never execute
# if the array contains a single element.
return -1

def main():
arr = [4, 1, 2, 1, 2]
ans = get_single_element(arr)
print("The single element is:", ans)

if __name__ == "__main__":
main()

```
```
``````

function getSingleElement(arr) {
// Size of the array:
const n = arr.length;

// Declare the hashmap.
// And hash the given array:
const hashmap = new Map();
for (let i = 0; i < n; i++) {
const num = arr[i];
hashmap.set(num, (hashmap.get(num) || 0) + 1);
}

// Find the single element and return the answer:
for (const [num, count] of hashmap) {
if (count === 1) {
return num;
}
}

// This line will never execute
// if the array contains a single element.
return -1;
}

function main() {
const arr = [4, 1, 2, 1, 2];
const ans = getSingleElement(arr);
console.log("The single element is:", ans);
}

main();

```
```

Output: The single element is: 4

Complexity Analysis

Time Complexity: O(N*logM) + O(M), where M = size of the map i.e. M = (N/2)+1. N = size of the array.
Reason: We are inserting N elements in the map data structure and insertion takes logM time(where M = size of the map). So this results in the first term O(N*logM). The second term is to iterate the map and search the single element. In Java, HashMap generally takes O(1) time complexity for insertion and search. In that case, the time complexity will be O(N) + O(M).

Note: The time complexity will be changed depending on which map data structure we are using. If we use unordered_map in C++, the time complexity will be O(N) for the best and average case instead of O(N*logM). But in the worst case(which rarely happens), it will be nearly O(N2).

Space Complexity: O(M) as we are using a map data structure. Here M = size of the map i.e. M = (N/2)+1.

Optimal Approach
Algorithm / Intuition

### Intuition:

Two important properties of XOR are the following:

XOR of two same numbers is always 0 i.e. a ^ a = 0. ←Property 1.
XOR of a number with 0 will result in the number itself i.e. 0 ^ a = a.  ←Property 2

Here all the numbers except the single number appear twice and so will form a pair. Now, if we perform XOR of all elements of the array, the XOR of each pair will result in 0 according to the XOR property 1. The result will be = 0 ^ (single number) = single number (according to property 2).

So, if we perform the XOR of all the numbers of the array elements, we will be left with a single number.

### Approach:

• We will just perform the XOR of all elements of the array using a loop and the final XOR will be the answer.

Dry run:

`Assume the given array is: [4,1,2,1,2]XOR of all elements = 4^1^2^1^2      = 4 ^ (1^1) ^ (2^2)      = 4 ^ 0 ^ 0 = 4^0 = 4Hence, 4 is the single element in the array.`
Code
``````

#include <bits/stdc++.h>
using namespace std;

int getSingleElement(vector<int> &arr) {
//size of the array:
int n = arr.size();

// XOR all the elements:
int xorr = 0;
for (int i = 0; i < n; i++) {
xorr = xorr ^ arr[i];
}
return xorr;
}

int main()
{
vector<int> arr = {4, 1, 2, 1, 2};
int ans = getSingleElement(arr);
cout << "The single element is: " << ans << endl;
return 0;
}
```
```
``````

import java.util.*;

public class tUf {
public static int getSingleElement(int []arr) {
//size of the array:
int n = arr.length;

// XOR all the elements:
int xorr = 0;
for (int i = 0; i < n; i++) {
xorr = xorr ^ arr[i];
}
return xorr;
}

public static void main(String args[]) {
int[] arr = {4, 1, 2, 1, 2};
int ans = getSingleElement(arr);
System.out.println("The single element is: " + ans);

}
}
```
```
``````

def getSingleElement(arr):
# XOR all the elements:
xorr = 0
for num in arr:
xorr ^= num
return xorr

def main():
arr = [4, 1, 2, 1, 2]
ans = getSingleElement(arr)
print("The single element is:", ans)

if __name__ == "__main__":
main()

```
```
``````

function getSingleElement(arr) {
// XOR all the elements:
let xorr = 0;
for (let i = 0; i < arr.length; i++) {
xorr = xorr ^ arr[i];
}
return xorr;
}

function main() {
let arr = [4, 1, 2, 1, 2];
let ans = getSingleElement(arr);
console.log("The single element is:", ans);
}

main();

```
```

Output: The single element is: 4

Complexity Analysis

Time Complexity: O(N), where N = size of the array.
Reason: We are iterating the array only once.

Space Complexity: O(1) as we are not using any extra space.

Video Explanation