GFG two only odd numbers present in an array.

 package com.company;


public class CWR_TwoOddOccurring_Gfg {

// Two odd occurring ---> mindBlowing
public static void findOdd(int[] arr, int n) {
int res1 = 0, res2 = 0, xor = 0;
for (int i = 0; i < n; i++) {
xor = xor ^ arr[i];
}
int r = xor & ~(xor-1);

for(int i = 0; i < n; i++){
if((arr[i] & r) != 0){
res1 = res1 ^ arr[i];
}
else
res2 = res2 ^ arr[i];
}
System.out.println(res1 + "\n" + res2);
}

public static void main(String[] args) {
int[] a = {3, 3, 4, 5, 6, 6};
findOdd(a,6);
// System.out.println();
}
}

Comments