fork(4) download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. int[] arr = {1, 2, 1, 4, 3, 1};
  6. int n = 6;
  7.  
  8. Map<Integer, Integer> ump = new HashMap<Integer, Integer>();
  9. for (int i = 0; i < n; ++i) {
  10. if (ump.containsKey(arr[i])) {
  11. ump.put(arr[i], ump.get(arr[i]) + 1);
  12. } else {
  13. ump.put(arr[i], 1);
  14. }
  15. }
  16.  
  17. int maxfreqsofar = 0;
  18. for (Map.Entry<Integer, Integer> entry : ump.entrySet()) {
  19. int freq = entry.getValue();
  20. if (freq > maxfreqsofar) {
  21. maxfreqsofar = freq;
  22. }
  23. }
  24.  
  25. System.out.println("The minimum number of operations required to make all elements equal is: " + (n - maxfreqsofar));
  26. }
  27. }
  28.  
Success #stdin #stdout 0.12s 48308KB
stdin
Standard input is empty
stdout
The minimum number of operations required to make all elements equal is: 3