Logic: Do Xor on the elements.
public class MainClass {
public static void main(String [] args)
{
int []a = new int []{1,1,2,3,2,4,4,4,4,1,1,5,6,5,1,6,1};
int res =a[0];
for(int i =1 ; i<a.length; i++)
{
res = res^a[i];
}
System.out.println(res);
}
}
Run time O(n)
ReplyDelete