Find the frequency of each element in a sorted array | Function to print the frequency of each element of the sorted array ; Stores the frequency of an element ; Traverse the array arr [ ] ; If the current element is equal to the previous element ; Increment the freq by 1 ; Otherwise , ; Update freq ; Print the frequency of the last element ; Given Input ; Function Call
function printFreq ( arr , N ) { let freq = 1 ; for ( let i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { freq ++ ; } else { document . write ( " " + parseInt ( arr [ i - 1 ] ) + " " + parseInt ( freq ) + " " ) ; freq = 1 ; } } document . write ( " " + parseInt ( arr [ N - 1 ] ) + " " + parseInt ( freq ) + " " ) ; } let arr = [ 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 ] ; let N = arr . length ; printFreq ( arr , N ) ;