Smallest element in an array that is repeated exactly ' k ' times . | finds the smallest number in arr [ ] that is repeated k times ; Since arr [ ] has numbers in range from 1 to MAX ; set count to 1 as number is present once ; If frequency of number is equal to ' k ' ; Driver code
< ? php function findDuplicate ( $ arr , $ n , $ k ) { $ MAX = 1000 ; $ res = $ MAX + 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > 0 ) { $ count = 1 ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ i ] == $ arr [ $ j ] ) $ count += 1 ; if ( $ count == $ k ) $ res = min ( $ res , $ arr [ $ i ] ) ; } } return $ res ; } $ arr = array ( 2 , 2 , 1 , 3 , 1 ) ; $ k = 2 ; $ n = count ( $ arr ) ; echo findDuplicate ( $ arr , $ n , $ k ) ; ? >