Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Monday, December 4, 2017

number without pair in no extra space


Idea is to use XOR 
a^a=0
a^a^b=b

Code:

import java.util.*;
import java.lang.*;
import java.io.*;
class dmg
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
    int n=ab.nextInt();
    int x=0;
    for(int i=0;i<n;++i)
   x= x^ab.nextInt();
    System.out.println(x);
}
}
}

0 Comments:

Post a Comment

Stats