Second most repeated word in a sequence | C # program to find out the second most repeated word ; Method to find the word ; Store all the words with its occurrence ; find the second largest occurrence ; Return string with occurrence equals to sec_max ; Driver method
using System ; using System . Collections . Generic ; class GFG { static String secMostRepeated ( List < String > seq ) { Dictionary < String , int > occ = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < seq . Count ; i ++ ) if ( occ . ContainsKey ( seq [ i ] ) ) occ [ seq [ i ] ] = occ [ seq [ i ] ] + 1 ; else occ . Add ( seq [ i ] , 1 ) ; int first_max = int . MinValue , sec_max = int . MinValue ; foreach ( KeyValuePair < String , int > entry in occ ) { int v = entry . Value ; if ( v > first_max ) { sec_max = first_max ; first_max = v ; } else if ( v > sec_max && v != first_max ) sec_max = v ; } foreach ( KeyValuePair < String , int > entry in occ ) { int v = entry . Value ; if ( v == sec_max ) return entry . Key ; } return null ; } public static void Main ( String [ ] args ) { String [ ] arr = { " ccc " , " aaa " , " ccc " , " ddd " , " aaa " , " aaa " } ; List < String > seq = new List < String > ( arr ) ; Console . WriteLine ( secMostRepeated ( seq ) ) ; } }