fork(8) download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. int[] a = new int[] { 1, 2, 4 };
  9. int[] b = new int[] {1, 3, 4, 1 };
  10.  
  11. System.out.println( duplicates( a, a.length ));
  12. System.out.println( duplicates( b, b.length ));
  13.  
  14. }
  15.  
  16. public static boolean duplicates (int [] x, int numElementsInX ) {
  17. Set<Integer> set = new HashSet<Integer>();
  18. for ( int i = 0; i < numElementsInX; ++i ) {
  19. if ( set.contains( x[i])) {
  20. return true;
  21. }
  22. else {
  23. set.add(x[i]);
  24. }
  25. }
  26. return false;
  27. }
  28.  
  29. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
false
true