# Finding Odd Occurrence of a Number in Java

An array of non-negative integers is given such that every number is occurring even number of times, barring one number which is occurring an odd number of times. The task is to find that number that is occurring an odd number of times.

Examples:

Input: a[] = {7, 4, 5, 4, 5, 7, 5, 9, 8, 9, 6, 8, 6}

Output: 5

Explanation: Apart from 5, all the other numbers are occurring even number of times. Note that the number 5 occurs 3 times in the input array, which is odd.

Input: a[] = {4, 3, 9, 3, 4, 9, 4, 7, 4, 7, 4}

Output: 4

Explanation: Apart from 4, all the other numbers are occurring even number of times. Note that the number 4 occurs 5 times in the input array, which is odd.

## Naive Approach

The straightforward solution is to use two loops. The outer loop will pick a number, and the inner loop will count the frequency of the occurrence of that number. If the frequency of the occurrence is even, then we discard that number. If the frequency of the occurrence is odd, then we return that number.

FileName: OddOccurrence.java

Output:

```In the following input array:
7 4 5 4 5 7 5 9 8 9 6 8 6
The number whose occurrence is odd is: 5

In the following input array:
4 3 9 3 4 9 4 7 4 7 4
The number whose occurrence is odd is: 4
```

Time Complexity: The above program uses two loops that have been nested. Therefore, the time complexity is O(n2), where n is the total number of elements present in the input array.

Space Complexity: The above program is not using any extra space. Therefore, the space complexity of the above program is O(1).

## Finding Odd Occurrence of a Number Using Array

We can use an array to store the frequency of the occurrence of the elements. Then, we can check the numbers whose frequency is odd. The following program demonstrates the same.

FileName: OddOccurrence1.java

Output:

```In the following input array:
7 4 5 4 5 7 5 9 8 9 6 8 6
The number whose occurrence is odd is: 5

In the following input array:
4 3 9 3 4 9 4 7 4 7 4
The number whose occurrence is odd is: 4
```

Time Complexity: The above program uses three loops. However, those loops are not nested. Therefore, the time complexity is O(n), where n is the total number of elements present in the input array.

Space Complexity: The above program uses some extra space. We see the usage of an array for storing the frequency of occurrence of elements. The space complexity of the above program is O(maxElement), where maxElement is the maximum element found in the input array.

The downside of this approach is the unused space in the array that stores the frequency. For example: for the array {7, 4, 5, 4, 5, 7, 5, 9, 8, 9, 6, 8, 6} the memory will be allocated for 10 elements in the array storeFreq[]. However, only storeFreq[4], storeFreq[5], storeFreq[6], storeFreq[7], storeFreq[8], and storeFreq[9] will be used. Thus, the space allocated for storeFreq[0], storeFreq[1], storeFreq[3], and many more goes in vain. The condition becomes, even more, worse if we get the input as {1000, 1, 1000}. In this case, we have to allocate the memory for the 1001 elements, out of which only memory of 2 elements (one for 1000 and another for 1) will be used. The rest of the other memory allocation is useless. To avoid this wastage of memory, we should be using a hash map.

## Finding Odd Occurrence of a Number Using HashMap

We can also use a hash map to store the frequency of the occurrence of the elements. After that, we can check the numbers whose frequency is odd. The following program illustrates the same.

FileName: OddOccurrence2.java

Output:

```In the following input array:
7 4 5 4 5 7 5 9 8 9 6 8 6
The number whose occurrence is odd is: 5

In the following input array:
4 3 9 3 4 9 4 7 4 7 4
The number whose occurrence is odd is: 4
```

Time Complexity: The above program is using two loops. However, those loops are not nested. Therefore, the time complexity is O(n), where n is the total number elements present in the input array.

Space Complexity: The above program is using some extra space. Notice the usage of the hash map in the program for storing the elements and their frequency of occurrence. It is obvious that the hash map cannot store the elements more than the elements present in the input array. Therefore, the space complexity of the above program is O(n), where n is the total number of elements present in the input array.

## Finding Odd Occurrence of a Number Using Bitwise XOR Operator

In this approach, we will use bitwise XOR (^) operation to find the odd occurrence of the number in the input array. The idea behind this approach is that XOR operation is commutative, i.e.,

All we have done is the rearrangement of numbers such that the same numbers are grouped together. Also, we know that the XOR of the any two same numbers is 0. Therefore,

7 ^ 7 = 0, 4 ^ 4 = 0, 5 ^ 5 ^ 5 = 0 ^ 5 = 5, 9 ^ 9 = 0, 8 ^ 8 = 0, 6 ^ 6 = 0

Thus, we have

7 ^ 7 ^ 4 ^ 4 ^ 5 ^ 5 ^ 5 ^ 9 ^ 9 ^ 8 ^ 8 ^ 6 ^ 6 = 0 ^ 0 ^ 5 ^ 0 ^ 0 ^ 0 = 5

The following program shows the same.

FileName: OddOccurrence3.java

Output:

```In the following input array:
7 4 5 4 5 7 5 9 8 9 6 8 6
The number whose occurrence is odd is: 5

In the following input array:
4 3 9 3 4 9 4 7 4 7 4
The number whose occurrence is odd is: 4
```

Time Complexity: The above program uses only one loop. Therefore, the time complexity is O(n), where n is the total number of elements present in the input array.

Space Complexity: The above program is not using any extra space. Therefore, the space complexity of the above program is O(1).

Next TopicJava Indentation