# C++ program to find element that appears once

This is not an efficient approach but just another way to get the desired results. If we add each number once and multiply the sum by 2, we will get twice the sum of each element of the array. Then we will subtract the sum of the whole array from the twice_sum and get the required number (which appears once in the array).
Array [] : [a, a, b, b, c, c, d]
Mathematical Equation = 2*(a+b+c+d) – (a + a + b + b + c + c + d)

In more simple words: 2(sum_of_array_without_duplicates) – (sum_of_array)*

let arr[] = {7, 3, 5, 4, 5, 3, 4} Required no = 2*(sum_of_array_without_duplicates) - (sum_of_array) = 2*(7 + 3 + 5 + 4) - (7 + 3 + 5 + 4 + 5 + 3 + 4) = 2* 19 - 31 = 38 - 31 = 7 (required answer)

As we know that set does not contain any duplicate element we will be using the set

Below is the implementation of above approach:

• C++

`// C++ program to find`

`// element that appears once`

`#include <bits/stdc++.h>`

`using` `namespace` `std;`

`// function which find number`

`int` `singleNumber(` `int` `nums[],` `int` `n)`

`{`

` ` `map<` `int` `,` `int` `> m;`

` ` `long` `sum1 = 0,sum2 = 0;`

` ` `for` `(` `int` `i = 0; i < n; i++)`

` ` `{`

` ` `if` `(m[nums[i]] == 0)`

` ` `{`

` ` `sum1 += nums[i];`

` ` `m[nums[i]]++;`

` ` `}`

` ` `sum2 += nums[i];`

` ` `}`

` `

` ` `// applying the formula.`

` ` `return` `2 * (sum1) - sum2;`

`}`

`// Driver code`

`int` `main()`

`{`

` ` `int` `a[] = {2, 3, 5, 4, 5, 3, 4};`

` ` `int` `n = 7;`

` ` `cout << singleNumber(a,n) << ` `"\n"` `;`

` ` `int` `b[] = {15, 18, 16, 18, 16, 15, 89};`

` ` `cout << singleNumber(b,n);`

` ` `return` `0;`

`}`

Output:

2 89

Another approach:

This is an efficient approach for finding the single element in a list of duplicate elements. In this approach, we are using binary search algorithm to find the single element in the list of duplicates elements. Before that, we need to make sure if the array is sorted. The first step is to sort the array because binary search algorithm wont work if the array is not sorted.

Now let us move to the binary search implementation:

There are two halfs that are created by the only single element present in the array which are left half and right half. Now if there are duplicates present in the left half, then the 1st instance of the duplicate element in the left half is an even index and the 2nd instance is an odd index. The opposite of the left half happens in the right half(1st instance is odd index and the second instance is even index). Now apply binary search algorithm:

1)The solution is to take two indexes of the array(low and high) where low points to array-index 0 and high points to array-index (array size-2). We take out the mid index from the values by (low+high)/2.

1. Now check if the mid index value falls in the left half or the right half. If it falls in the left half then we change the low value to mid+1 and if it falls in the right half, then we change the high index to mid-1. To check it , we used a logic (if(arr[mid]==arr[mid^1]). If mid is an even number then mid^1 will be the next odd index , and if the condition gets satisfied, then we can say that we are in the left index,else we can say we are in the right half. But if mid is an odd index, then mid^1 takes us to mid-1 which is the previous even index , which is gets equal means we are in the right half else left half.

2. This is done because the aim of this implementation is to find the single element in the list of duplicates. It is only possible if low value is more than high value because at that moment low will be pointing to the index that contains the single element in the array.

3. After the loop ends, we return the value with low index.

• C++

`#include <bits/stdc++.h>`

`using` `namespace` `std;`

`int` `singleelement(` `int` `arr[], ` `int` `n)`

`{`

` ` `int` `low = 0, high = n - 2;`

` ` `int` `mid;`

` ` `while` `(low <= high) {`

` ` `mid = (low + high) / 2;`

` ` `if` `(arr[mid] == arr[mid ^ 1]) {`

` ` `low = mid + 1;`

` ` `}`

` ` `else` `{`

` ` `high = mid - 1;`

` ` `}`

` ` `}`

` ` `return` `arr[low];`

`}`

`int` `main()`

`{`

` ` `int` `arr[] = { 2, 3, 5, 4, 5, 3, 4 };`

` ` `int` `size = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);`

` ` `sort(arr, arr + size);`

` ` `cout << singleelement(arr, size);`

` ` `return` `0;`

`}`

Output:

2

The time complexity of the solution is O(N log(N))+O(log N) and its space complexity is O(1).