Find the number of occurrences of a character upto preceding position | Function to find the number of occurrences of a character at position P upto p - 1 ; Iterate over the string ; Store the Occurrence of same character ; Increase its frequency ; Return the required count ; Driver code ; Function call
function countOccurrence ( s , position ) { let alpha = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { alpha [ i ] = 0 ; } let b = new Array ( s . length ) ; for ( let i = 0 ; i < s . length ; i ++ ) { b [ i ] = alpha [ s [ i ] . charCodeAt ( 0 ) - 97 ] ; alpha [ s [ i ] . charCodeAt ( 0 ) - 97 ] ++ ; } return b [ position - 1 ] ; } let s = " " ; p = 9 ; document . write ( countOccurrence ( s , p ) ) ;