-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindeTheMode
69 lines (47 loc) · 1.19 KB
/
FindeTheMode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*Find the mode
Problem Statement
The goal is to read in a list of integers into an array and output the one which occurs most frequently.
If there are two or more values that occur most frequently then choose the one which was earliest in the list.
Input Format
The first line contains N, the number of inputs. The second line contains N integers separated by a space.
Output Format
The mode, that is, the number which occurs most frequently in the list.
Constraints
1≤N≤1000
-1000≤A[i]≤1000
Sample Input
7
15 18 3 2 -5 6 2
Sample Output
2*/
import java.util.Scanner;
public class FindTheMode {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int x=sc.nextInt();
int[]tab=new int[x];
int[]tab2=new int[x];
for(int i=0;i<x;i++)
{
tab[i]=sc.nextInt();
}
for(int i=0;i<x;i++)
{int count=0;
for(int j=0;j<x;j++)
{
if(tab[i]==tab[j]){
count++;
}
tab2[i]=count;
}
}
int max=tab[0];
int pom=0;
for(int i=0;i<x;i++)
{
if(max<tab2[i]){max=tab2[i]; pom=i;
}
}
System.out.println(tab[pom]);
}
}