Find Unique Number in Array

 If all numbers repeated except one:

Use XOR operator

public class progprac {
public static void main(String[] args) {
int[] arr = {2, 3, 2, 4, 4};
int result=0;

for(int num : arr) {
result = result ^ num;
}
System.out.println("Result="+result);
}
}




No comments:

Post a Comment

Please comment below to feedback or ask questions.