Largest element in the array that is repeated exactly k times | Function that finds the largest element which is repeated ' k ' times ; sort the array ; if the value of ' k ' is 1 and the largest appears only once in the array ; counter to count the repeated elements ; check if the element at index ' i ' is equal to the element at index ' i + 1' then increase the count ; else set the count to 1 to start counting the frequency of the new number ; if the count is equal to k and the previous element is not equal to this element ; if there is no such element ; Driver code ; find the largest element that is repeated K times
< ? php function solve ( & $ arr , $ n , $ k ) { sort ( $ arr ) ; if ( $ k == 1 && $ arr [ $ n - 2 ] != $ arr [ $ n - 1 ] ) { echo $ arr [ $ n - 1 ] ; echo ( " STRNEWLINE " ) ; return ; } $ count = 1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ arr [ $ i ] == $ arr [ $ i + 1 ] ) $ count ++ ; else $ count = 1 ; if ( $ count == $ k && ( $ i == 0 || ( $ arr [ $ i - 1 ] != $ arr [ $ i ] ) ) ) { echo ( $ arr [ $ i ] ) ; echo ( " STRNEWLINE " ) ; return ; } } echo ( " No ▁ such ▁ element " ) ; echo ( " STRNEWLINE " ) ; } $ arr = array ( 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 ) ; $ k = 2 ; $ n = sizeof ( $ arr ) ; solve ( $ arr , $ n , $ k ) ; ? >