Mode in a stream of integers ( running integers ) | Java implementation of the above approach ; Function that prints the Mode values ; Map used to map integers to its frequency ; To store the maximum frequency ; To store the element with the maximum frequency ; Loop used to read the elements one by one ; Updates the frequency of that element ; Checks for maximum Number of occurrence ; Updates the maximum frequency ; Updates the Mode ; Driver Code ; Function Call
import java . util . * ; public class GFG { public static void findMode ( int [ ] a , int n ) { Map < Integer , Integer > map = new HashMap < > ( ) ; int max = 0 ; int mode = 0 ; for ( int i = 0 ; i < n ; i ++ ) { map . put ( a [ i ] , map . getOrDefault ( a [ i ] , 0 ) + 1 ) ; if ( map . get ( a [ i ] ) >= max ) { max = map . get ( a [ i ] ) ; mode = a [ i ] ; } System . out . print ( mode ) ; System . out . print ( " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 7 , 3 , 2 , 5 } ; int n = arr . length ; findMode ( arr , n ) ; } }