Advertisement
Guest User

Untitled

a guest
Jun 8th, 2022
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class mockexam3_task1 {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7.  
  8. int numbercount = Integer.parseInt(scanner.nextLine());
  9.  
  10. int[] array1 = new int[numbercount];
  11.  
  12. for (int i = 0; i < numbercount; i++) {
  13. array1[i] = Integer.parseInt(scanner.nextLine());
  14. if (numbercount < 2) {
  15. System.out.println(array1[i]);
  16. return;
  17. }
  18. }
  19.  
  20. Arrays.sort(array1);
  21.  
  22. int targetNumber = 0;
  23. int maxCount = 0;
  24. int position = 0;
  25. int min = 0;
  26.  
  27. for (int currentNumber = 0; currentNumber < array1.length; currentNumber++) {
  28. int currentCount = 0;
  29. for (int i = position; i < array1.length; i++) {
  30. if (array1[currentNumber] == array1[i]) {
  31. currentCount++;
  32. } else {
  33. break;
  34. }
  35. position++;
  36. }
  37. if (currentCount > maxCount && currentCount > 1) {
  38. maxCount = currentCount;
  39. targetNumber = array1[currentNumber];
  40. } else if (currentCount == maxCount) {
  41. min = Math.min(array1[currentNumber], targetNumber);
  42. }
  43.  
  44. }
  45. if (min == 0) {
  46. System.out.println(targetNumber);
  47. } else {
  48. System.out.println(min);
  49. }
  50.  
  51.  
  52. }
  53. }
  54.  
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement