fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. int n = scanner.nextInt();
  7. int[] arr = new int[n];
  8.  
  9. for (int i = 0; i < n; i++) {
  10. arr[i] = scanner.nextInt();
  11. }
  12.  
  13. int maxElement = 0, maxCount = 0, minElement = 0, minCount = Integer.MAX_VALUE;
  14.  
  15. for (int i = 0; i < n; i++) {
  16. int cnt = 0;
  17. for (int j = 0; j < n; j++) {
  18. if (arr[i] == arr[j]) {
  19. cnt++;
  20. }
  21. }
  22.  
  23. if (maxCount < cnt) {
  24. maxCount = cnt;
  25. maxElement = arr[i];
  26. }
  27.  
  28. if (minCount > cnt) {
  29. minCount = cnt;
  30. minElement = arr[i];
  31. }
  32. }
  33.  
  34. System.out.println("Max Element: " + maxElement + ", Max Count: " + maxCount);
  35. System.out.println("Min Element: " + minElement + ", Min Count: " + minCount);
  36. }
  37. }
Success #stdin #stdout 0.14s 58864KB
stdin
5
1 3 4 6 7
stdout
Max Element: 1, Max Count: 1
Min Element: 1, Min Count: 1