Given a non-empty array of integers, each element appears twice except one that appears only once. Find the element that appears only once. Example 1: Input: [2,2,1] Output: 1 Example 2: Input: [4,1,2,1,2] Output: 4
public class Test37 {
public static void main(String[] args) {
int[] arr={2,2,1};
int[] arr2={4,1,2,1,2};
findTwe(arr);
System.out.println();
findTwe(arr2);
}
private static void findTwe(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int tmp=0;
for (int j = 0; j < arr.length; j++) {
if(arr[i]==arr[j]){
tmp++;
}
}
if(tmp==1){
System.out.println(arr[i]);
}
}
}
}
Achieving the result: