Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class MostFrequent{
  3. public static void main(String[] args) {
  4. Scanner in= new Scanner(System.in);
  5. int N = in.nextInt();
  6. int[] a = new int[N];
  7. for(int i=0;i<N;i++){
  8. a[i]=in.nextInt();
  9. }
  10. int maxLength = 1;
  11. int currentLength = 1;
  12. int mostFrequentNumber = a[0];
  13.  
  14. for (int i = 0; i < a.length; i++) {
  15.  
  16. int currentNumber = a[i];
  17. for (int j = i+1; j < a.length; j++) {
  18.  
  19. if(currentNumber == a[j]){
  20. currentLength++;
  21. }
  22.  
  23. }
  24.  
  25. if(currentLength > maxLength){
  26. maxLength = currentLength;
  27. mostFrequentNumber = currentNumber;
  28. }
  29.  
  30. currentLength = 1;
  31.  
  32. }
  33.  
  34. System.out.println(mostFrequentNumber+"("+maxLength+"times)");
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement