The problem with this code is that it will print out 5 9 9, instead of just 5,9. Because there is a third 9 in the array. What am I missing?
Edit: I need to write a function that will get the duplicates from the array given. I am trying to do this, but it is printing out a 5,9,9 instead of 5,9.
Edit 2: Well I figured it out after reading up on HashSet and got it to work using the code below. I hope this can help others with the same problem.
import java.util.HashSet;
public class Duplicator {
/**
* @param args
*/
public static void main(String[] args) {
int[] a = {3,5,5,8,9,9,9};
HashSet<Integer> hash = new HashSet<Integer>();
for(int i = 0; i < a.length; i++){
for(int j = i+1; j< a.length; j++){
if(a[i] == a[j]){
hash.add(a[i]);
}
}
}
System.out.println(hash);
}
}