Largest element in the array that is repeated exactly k times | Python implementation of above approach ; Function that finds the largest element that occurs exactly ' k ' times ; store the frequency of each element ; to store the maximum element ; if current element has frequency ' k ' and current maximum hasn 't been set ; set the current maximum ; if current element has frequency ' k ' and it is greater than the current maximum ; change the current maximum ; if there is no element with frequency 'k ; print the largest element with frequency 'k ; Driver code ; find the largest element that is repeated K times
import sys NEW_LINE def solve ( arr , n , k ) : NEW_LINE INDENT m = { } ; NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( arr [ i ] in m . keys ( ) ) : NEW_LINE INDENT m [ arr [ i ] ] += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT m [ arr [ i ] ] = 1 ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT max = sys . maxsize ; NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( m [ arr [ i ] ] == k and max == sys . maxsize ) : NEW_LINE INDENT max = arr [ i ] ; NEW_LINE DEDENT elif ( m [ arr [ i ] ] == k and max < arr [ i ] ) : NEW_LINE INDENT max = arr [ i ] ; NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( max == sys . maxsize ) : NEW_LINE INDENT print ( " No ▁ such ▁ element " ) ; NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT else : NEW_LINE INDENT print ( max ) ; NEW_LINE DEDENT DEDENT arr = [ 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 ] NEW_LINE k = 4 ; NEW_LINE n = len ( arr ) NEW_LINE solve ( arr , n , k ) NEW_LINE