-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ19.java
55 lines (44 loc) · 1.32 KB
/
Q19.java
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
import java.util.*;
public class Q19 {
public static void maxFrequency(int[] nums, int k){
boolean visited[] = new boolean[k];
int maxfreq = 0, minfreq = k;
int maxE = 0, minE = 0;
for(int i=0; i<k; i++){
if(visited[i] == true){
continue;
}
int count = 1;
for(int j=i+1; j<k; j++){
if(nums[i] == nums[j])
visited[j] = true;
count ++;
}
if (count > maxfreq) {
maxE = nums[i];
maxfreq = count;
}
if(count < minfreq){
minE = nums[i];
minfreq = count;
}
}
System.out.println("The highest frequency element is: " + maxE);
System.out.println("The lowest frequency element is: " + minE);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i =0; i<n; i++){
arr[i] = sc.nextInt();
}
// int k = sc.nextInt();
maxFrequency(arr, n);
}
}
// Input: 6
// 10 5 10 15 10 5
// Output :
// The highest frequency element is: 10
// The lowest frequency element is: 15