I am not familiar with Java, but I have a question about a Java code which is supposed to find the first non-repeat element in an array.
Hashtable<Integer, Boolean> positions = new Hashtable<Integer,Boolean>();
int[] elements = {2, 2, 4, 5, 1, 6, 0, 9, 1, 4, 5, 10};
for(int i=0; i<elements.length; i++) {
if(positions.containsKey(elements[i])) {
if(positions.get(elements[i]).booleanValue() == false) {
positions.put(elements[i],true);
}
}
else {
positions.put(elements[i], false);
}
}
for(int j=0; j<elements.length; j++) {
if (positions.get(elements[j]).booleanValue() == false) {
System.out.println("First unique element is: " + elements[j] + " at position " + j);
break;
}
}
For the first 2 in the array, we put 2 and false, for the second we will put 2 and true.
My question is will the 2 and true overwrite the 2 and false or they both exist in the Hashtable? thanks