Smallest element in an array that is repeated exactly ' k ' times . | javascript program to find smallest number in array that is repeated exactly ' k ' times . ; finds the smallest number in arr that is repeated k times ; Computing frequencies of all elements ; Finding the smallest element with frequency as k ; If frequency of any of the number is equal to k starting from 0 then return the number ; Driver code
var MAX = 1000 ; function findDuplicate ( arr , n , k ) { var freq = Array . from ( { length : MAX } , ( _ , i ) => 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 1 && arr [ i ] > MAX ) { document . write ( " " ) ; return - 1 ; } freq [ arr [ i ] ] += 1 ; } for ( var i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] == k ) return i ; } return - 1 ; } var arr = [ 2 , 2 , 1 , 3 , 1 ] ; var k = 2 ; var n = arr . length ; document . write ( findDuplicate ( arr , n , k ) ) ;