Given an integer array where every element appears an even number of times, except one element which appears an odd number of times. If the identical elements appear in pairs in the array and there cannot be more than two consecutive occurrences of an element, find the odd occurring element in logarithmic time and constant space.

For instance, both these arrays are invalid – {1, 2, 1} and {1, 1, 2, 2, 2, 3, 3}. The first one doesn’t have identical elements appear in pairs, and the second one contains three consecutive instances of an element. On the other hand, the array {2, 2, 3, 3, 2, 2, 4, 4, 3, 1, 1} is valid, and the odd occurring element present in it is 3.

Practice this problem

 
A naive solution is to sort the array and count each element’s occurrence by traversing the sorted array. We return the element with the odd count. The time complexity of this solution is O(n.log(n)), where n is the size of the input.

The above approach is demonstrated below in C++, Java, and Python:

C++


Download  Run Code

Output:

The odd occurring element is 3

Java


Download  Run Code

Output:

The odd occurring element is 3

Python


Download  Run Code

Output:

The odd occurring element is 3

We can solve this problem in linear time using the XOR operator. The idea is to take XOR of all array elements. The even occurring elements will cancel each other, and only the odd occurring elements are left. This approach is demonstrated below in C, Java, and Python:

C


Download  Run Code

Output:

The odd occurring element is 3

Java


Download  Run Code

Output:

The odd occurring element is 3

Python


Download  Run Code

Output:

The odd occurring element is 3

We can even solve this problem in O(log(n)) time.

As per problem constraints, identical elements appear in pairs in the array, and there cannot be more than two consecutive occurrences of any element. So, there must be a single occurrence of the odd element somewhere in the array. We can find this odd occurrence using the binary search algorithm.

Consider the following array with their positions:

 
If we carefully observe, each pair of the element before the odd occurring element has the first occurrence at an even index and the second occurrence at an odd index. And, each pair of the element after the odd occurrence has the first occurrence at the odd index and the second occurrence at the even index.

We can use the above observation to determine which side of the mid-index the odd occurring element lies. Now, two cases arise:

  1. The mid-index is odd: If the element before the mid-index is the same as the middle element, the odd element lies on the right side; otherwise, it lies on the left side.
  2. The mid-index is even: If the element next to the mid-index is the same as the middle element, the odd element lies on the right side; otherwise, it lies on the left side.

The algorithm can be implemented as follows in C, Java, and Python:

C


Download  Run Code

Output:

The odd occurring element is 3

Java


Download  Run Code

Output:

The odd occurring element is 3

Python


Download  Run Code

Output:

The odd occurring element is 3