I am stuck in the following program:
I have an integer input array that has only one non-duplicate number, for example {1,1,3,2,3}. The output should display a non-duplicate element, i.e. 2.
So far I have done the following:
public class Solution {
public int singleNumber(int[] arr){
int size = arr.length;
int temp = 0;
int result = 0;
boolean flag = true;
int[] arr1 = new int[size];
for(int i=0;i<size;i++){
temp = arr[i];
for(int j=0;j<size;j++){
if(temp == arr[j]){
if(i != j)
flag = false;
break;
}
}
}
return result;
}
public static void main(String[] args) {
int[] a = {1,1,3,2,3};
Solution sol = new Solution();
System.out.println("SINGLE NUMBER : "+sol.singleNumber(a));
}
}
Constraining a solution in an array is preferable. Avoid using collections, cards.
source
share