fork(4) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int[] a = {3,5,6,3,3,5};
  13.  
  14. Integer[] indices = new Integer[a.length];
  15. for (int i = 0; i < a.length; ++i) indices[i] = i;
  16.  
  17. Arrays.sort(indices, (i, j) -> Integer.compare(a[i], a[j]));
  18.  
  19. int count = 0;
  20. int i = 0;
  21. while (i < indices.length) {
  22. int start = i;
  23. while (++i < indices.length && a[indices[i]] == a[indices[start]]);
  24.  
  25. int thisCount = i - start;
  26. count += thisCount * (thisCount - 1) / 2;
  27. }
  28. System.out.println(count);
  29. }
  30. }
Success #stdin #stdout 0.12s 4386816KB
stdin
Standard input is empty
stdout
4