fork download
  1. import java.util.*;
  2.  
  3. /* Name of the class has to be "Main" only if the class is public. */
  4. class Ideone
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. int[] strArray = new int[] {1,1, 2, 3, 2, 2, 3, 4, 7, 5, 4};
  9. Set<Integer> duplicates = checkDuplicate(strArray);
  10. System.out.printf("Duplicates: %s\n", duplicates);
  11. }
  12. private static Set<Integer> checkDuplicate(int[] intArray)
  13. {
  14. Set<Integer> duplicates = new HashSet<Integer>();
  15. Set<Integer> tmp = new HashSet<Integer>();
  16. for(Integer i: intArray)
  17. {
  18. if(!tmp.add(i))
  19. {
  20. duplicates.add(i);
  21. }
  22. }
  23. return duplicates;
  24. }
  25. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
Duplicates: [1, 2, 3, 4]