Find the most valued alphabet in the String | JavaScript implementation of the approach ; Function to return the maximum valued alphabet ; To store the first and the last occurrence of all the characters ; Set the first and the last occurrence of all the characters to - 1 ; Update the occurrences of the characters ; Only set the first occurrence if it hasn 't already been set ; To store the result ; For every alphabet ; If current alphabet doesn 't appear  in the given String ; If the current character has the highest value so far ; Driver code
const MAX = 26 ; function maxAlpha ( str , len ) { var first = new Array ( MAX ) ; var last = new Array ( MAX ) ; for ( var i = 0 ; i < MAX ; i ++ ) { first [ i ] = - 1 ; last [ i ] = - 1 ; } for ( var i = 0 ; i < len ; i ++ ) { var index = str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ; if ( first [ index ] === - 1 ) first [ index ] = i ; last [ index ] = i ; } var ans = - 1 , maxVal = - 1 ; for ( var i = 0 ; i < MAX ; i ++ ) { if ( first [ i ] === - 1 ) continue ; if ( last [ i ] - first [ i ] > maxVal ) { maxVal = last [ i ] - first [ i ] ; ans = i ; } } return String . fromCharCode ( ans + " " . charCodeAt ( 0 ) ) ; } var str = " " ; var len = str . length ; document . write ( maxAlpha ( str , len ) ) ;