1+ import java .util .*; // IMPORTING JAVA PACKAGES
2+
3+ class CodingChallenge { //DECLARING THE CLASS CODINGCHALLENGE
4+
5+ public static void main (String [] args ) { //DECLARING THE MAIN METHOD
6+
7+ int [] num = {2 ,6 ,6 ,2 ,8 ,0 ,7 ,6 ,1 ,4 ,6 ,3 ,6 }; //INTIALISING THE ARRAY
8+
9+ for (int i =0 ; i <num .length ; i ++) { // GO THROUGH THE ARRAY TO FIND THE MOST FREQUENT ARRAY
10+
11+ }
12+ Arrays .sort (num ); //SORT THE ARRAY IN ASCENDING ORDER
13+
14+ int n =num .length ; // INITIALISE AND DECLARE N AS THE LENGTH OF THE ARRAY
15+
16+ int countofmax = 1 ;
17+ int temp = num [0 ];
18+ int count = 1 ;
19+
20+ for (int i = 1 ; i < num .length ; i ++)
21+ {
22+ if (num [i ] == num [i - 1 ])
23+ count ++;
24+ else
25+ {
26+ if (count > countofmax )
27+ {
28+ countofmax = count ;
29+ temp = num [i - 1 ];
30+ }
31+ count = 1 ;
32+ }
33+ }
34+
35+
36+ if (count > countofmax )
37+ {
38+ countofmax = count ;
39+ temp = num [n - 1 ];
40+ }
41+
42+ int j ;
43+ for ( j = 0 ; j <num .length ; j ++) {
44+
45+ }
46+ Arrays .sort (num );
47+ //DECLARE CURRENT ELEMENT COUNT, MIN ELEMET COUNT AND LEAST FREQUENT
48+ int current_element_count = 1 ;
49+ int min_element_count = num .length +1 ;
50+ int least_frequent_element = num [0 ];
51+
52+
53+ // gO THROUGH THE ARRAY TO SEARCH FOR THE LEAST FREQUENT ARRAY
54+ if (num [j -1 ] == num [j -2 ])
55+ {
56+ current_element_count ++;
57+ }
58+ else {
59+ if (current_element_count < min_element_count )
60+ {
61+ min_element_count = current_element_count ;
62+ least_frequent_element = num [j -1 ];
63+ }
64+ current_element_count = 1 ;
65+ }
66+
67+ if (current_element_count < min_element_count )
68+ {
69+ min_element_count = current_element_count ;
70+ least_frequent_element = num [num .length -1 ];
71+ }
72+
73+
74+
75+
76+ System .out .println ("The most frequent element is:" +temp ); // DISPLAY THE MOST FREQUENT ELEMENT
77+ System .out .println ("Least frequent element: " + least_frequent_element ); //DISPLAY LEAST FREQUENT ELEMENT
78+ }
79+ }
80+
0 commit comments