Issue
I think the problem is int this line "num = A[i]" I have tried to assign the num as zero. But could not accomplish.
public static int solution(int A[]){
int num = A[0];
int count = 0;
for(int i = 0; i<A.length ; i++) {
for(int j = 0; j<A.length; j++) {
if(A[i] == A[j]) {
count++;
}
if(count%2 != 0)
num = A[i];
else
count = 0;
}
}
return num;
}
public static void main(String[] args) {
int arr[] = {10,22,345,22,345,10,5};
System.out.println(solution(arr));
}
Solution
You could try something like this and track multiple solutions:
Set<Integer> odds = new HashSet<Integer>();
for (int i : A) {
if (odds.contains(i)) {
odds.remove(i);
} else {
odds.add(i);
}
}
System.out.println(odds);
Answered By - Ryan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.