Mode in a stream of integers ( running integers ) | C # 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
using System ; using System . Collections . Generic ; class GFG { public static void findMode ( int [ ] a , int n ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int max = 0 ; int mode = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . ContainsKey ( a [ i ] ) ) { map [ a [ i ] ] = map [ a [ i ] ] + 1 ; } else { map . Add ( a [ i ] , 1 ) ; } if ( map [ a [ i ] ] >= max ) { max = map [ a [ i ] ] ; mode = a [ i ] ; } Console . Write ( mode ) ; Console . Write ( " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 3 , 2 , 5 } ; int n = arr . Length ; findMode ( arr , n ) ; } }