how to find the mode of an array in java
Java Program To Calculate Way – In this article, we will brief in on the mentioned means to calculate the mode of given data in Java Programming. Following java lawmaking has been written in multiple ways for easy sympathise.
The ways used in this article are equally follows:
- Using Scanner Class
- Using Static Method
- Using Command Line Arguments
- Using Split Course
Just like Median, Mode is the value in a given gear up of probability distributions or populations which is repeated the well-nigh number of times.
Manner specifies the maximum occurring value in the gear up of options given. If at that place is no value repeating more the others, and so the ready has no mode.
As you can see in the example given above, this is a listing having multiple values.
x, 20, 15, 20, 25, xxx, 35, xx, 20, 30, 15.
It is clearly visible that the number xx is repeated iv times. Hence, the mode of this list is 20.
Mode Java Code
Scanner Class in Java is very well known for reading inputs at runtime. For this problem, we require an array to store values and hence, nosotros outset take input of length/size of the array (n) followed by which we read the elements in the array (a).
To find the manner, nosotros offset require to discover the number of times each element of the array is repeated. To store this count, we demand another assortment (b) of same size.
The side by side step is, we accept each element of the array and compare information technology with all the elements of the array.
If the element is equal to another element in the assortment then the count is incremented (variable used hither is c) and the terminal value of count (c) is stored in array b for the same index (i) as the chemical element compared.
To avoid redundancy and repetition of counting the same element twice when we observe some other chemical element equal to the chemical element compared, equally shortly equally count is incremented the another element (a[j]) value is made equal to -ane.
And not merely that, every fourth dimension nosotros encounter element -1 the count for that alphabetize in array b (b[i]) is automatically equated to 0 as goes to the next element.
Once all the elements in array (a) are compared and all the count values in some other array (b) are updated, then the job is to find the index having the largest count. For this, we initially set the index to aught and begin comparing with all the other elements.
If an chemical element it institute to be greater than this, then the alphabetize is updated to the index of this newly found largest element. This procedure continues till we reach the end of assortment.
The alphabetize of the largest count is then used to find the element in array (a) positioned at the aforementioned index. This element is the manner that nosotros are looking for.
1 2 3 4 5 6 7 8 9 10 11 12 thirteen 14 15 16 17 18 xix 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 | import java . util . Scanner ; class Way { public static void main ( String args [ ] ) { Scanner sc = new Scanner ( Organization . in ) ; System . out . println ( "enter a number" ) ; int north = sc . nextInt ( ) , c ; int [ ] a = new int [ north ] ; int [ ] b = new int [ northward ] ; System . out . println ( "enter " + north + " elements" ) ; double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } { for ( int i = 0 ; i < n ; i ++ ) { c = 1 ; if ( a [ i ] == - i ) b [ i ] = 0 ; else { for ( int j = i + ane ; j < due north ; j ++ ) { if ( a [ i ] == a [ j ] ) { c ++ ; a [ j ] = - 1 ; } } b [ i ] = c ; } } int one thousand = b [ 0 ] ; for ( int i = i ; i < n ; i ++ ) { if ( b [ i ] >= m ) yard = b [ i ] ; } System . out . println ( "mode:" ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( b [ i ] == m ) System . out . println ( a [ i ] ) ; } } } } |
Output
enter a number iv enter four elements i 1 2 2 mode : ane 2 |
Using Static Method
Static method makes use of the same logic referred above. The difference arises in the writing of code. Unlike the higher up, here we exercise not write the entire code in the same principal method but instead, separate it up.
There volition exist another static method in the same class as that of main method which volition consists of the set of statements used for finding the count of each element in the assortment and then finding the largest count to detect the well-nigh repeated number in that array i.e., the fashion.
But, to give this method all the inputs is taken care of in the main method using the scanner grade.
The scanner class reads the size of array and the elements of array.
This input is this passed to the static method (modeCal) which will be chosen in the primary method.
This executes the ready of instructions written within it and returns the output mode which is then displayed past using output argument in the main method.
1 two iii iv v 6 seven 8 9 10 xi 12 13 xiv fifteen xvi 17 eighteen 19 20 21 22 23 24 25 26 27 28 29 thirty 31 32 33 34 35 36 37 38 39 forty 41 42 43 44 45 46 47 48 49 l 51 52 53 54 55 56 57 58 59 threescore 61 | import java . util . Scanner ; class Mode { public static void principal ( Cord args [ ] ) { Scanner sc = new Scanner ( Organisation . in ) ; System . out . println ( "enter a number" ) ; int n = sc . nextInt ( ) , c ; int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; Organization . out . println ( "enter " + n + " elements" ) ; double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } modeCal ( n , a , b ) ; } static void modeCal ( int n , int a [ ] , int b [ ] ) { int m = 0 , c ; for ( int i = 0 ; i < due north ; i ++ ) { c = one ; if ( a [ i ] == - i ) b [ i ] = 0 ; else { for ( int j = i + ane ; j < n ; j ++ ) { if ( a [ i ] == a [ j ] ) { c ++ ; a [ j ] = - one ; } } b [ i ] = c ; } if ( i >= one ) { if ( i == ane ) k = b [ 0 ] ; if ( b [ i ] >= chiliad ) m = b [ i ] ; } } System . out . println ( "manner:" ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( b [ i ] == m ) Arrangement . out . println ( a [ i ] ) ; } } } |
Output:
enter a number iii enter 3 elements ii 3 four mode : 2 3 four |
Using Command Line Arguments
In the command line arguments, while giving the control to run the code along with the proper noun of the code we likewise give the size of array (arg[0]) with a space followed past the information values of the array (arg[i+n]).
These arguments are taken by principal method are are stored in respective variables like the size of array here is stored in integer variable 'n' and the data values are stored in array (a) of size n. While storing we also catechumen them into their corresponding information blazon using parsing method similar
int n = Integer . parseInt ( args [ 0 ] ) ; |
After storing these inputs, we perform the same set of instructions equally used above.
one ii iii 4 5 6 seven viii 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 | import java . util . Scanner ; class Mode { public static void main ( String args [ ] ) { int n = Integer . parseInt ( args [ 0 ] ) ; System . out . println ( "n=" + north ) ; int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; double sum = 0 ; System . out . println ( + north + " elements are" ) ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = Integer . parseInt ( args [ i + 1 ] ) ; System . out . println ( a [ i ] ) ; } modeCal ( n , a , b ) ; } static void modeCal ( int n , int a [ ] , int b [ ] ) { int yard = 0 , c ; for ( int i = 0 ; i < n ; i ++ ) { c = 1 ; if ( a [ i ] == - i ) b [ i ] = 0 ; else { for ( int j = i + i ; j < due north ; j ++ ) { if ( a [ i ] == a [ j ] ) { c ++ ; a [ j ] = - 1 ; } } b [ i ] = c ; } if ( i >= i ) { if ( i == ane ) k = b [ 0 ] ; if ( b [ i ] >= m ) chiliad = b [ i ] ; } } System . out . println ( "style:" ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( b [ i ] == g ) Organisation . out . println ( a [ i ] ) ; } } |
Source: https://javatutoring.com/java-mode-program/
Posted by: keatonhalk1956.blogspot.com
0 Response to "how to find the mode of an array in java"
Post a Comment