-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodingChallenge.java
More file actions
80 lines (60 loc) · 1.94 KB
/
CodingChallenge.java
File metadata and controls
80 lines (60 loc) · 1.94 KB
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
70
71
72
73
74
75
76
77
78
79
80
import java.util.*; // IMPORTING JAVA PACKAGES
class CodingChallenge { //DECLARING THE CLASS CODINGCHALLENGE
public static void main (String [] args) { //DECLARING THE MAIN METHOD
int [] num = {2,6,6,2,8,0,7,6,1,4,6,3,6}; //INTIALISING THE ARRAY
for(int i=0; i<num.length; i++) { // GO THROUGH THE ARRAY TO FIND THE MOST FREQUENT ARRAY
}
Arrays.sort(num); //SORT THE ARRAY IN ASCENDING ORDER
int n=num.length; // INITIALISE AND DECLARE N AS THE LENGTH OF THE ARRAY
int countofmax = 1;
int temp = num[0];
int count = 1;
for (int i = 1; i < num.length; i++)
{
if (num[i] == num[i - 1])
count++;
else
{
if (count > countofmax)
{
countofmax = count;
temp = num[i - 1];
}
count = 1;
}
}
if (count > countofmax)
{
countofmax = count;
temp = num[n - 1];
}
int j;
for( j = 0; j<num.length; j++) {
}
Arrays.sort(num);
//DECLARE CURRENT ELEMENT COUNT, MIN ELEMET COUNT AND LEAST FREQUENT
int current_element_count = 1;
int min_element_count = num.length+1;
int least_frequent_element = num[0];
// gO THROUGH THE ARRAY TO SEARCH FOR THE LEAST FREQUENT ARRAY
if(num[j-1] == num[j-2])
{
current_element_count++;
}
else {
if(current_element_count < min_element_count)
{
min_element_count = current_element_count;
least_frequent_element = num[j-1];
}
current_element_count = 1;
}
if(current_element_count < min_element_count)
{
min_element_count = current_element_count;
least_frequent_element = num[num.length-1];
}
System.out.println("The most frequent element is:"+temp); // DISPLAY THE MOST FREQUENT ELEMENT
System.out.println("Least frequent element: " + least_frequent_element); //DISPLAY LEAST FREQUENT ELEMENT
}
}