DEV Community

Eric See
Eric See

Posted on

Algorithm - Find Largest Upper Case Character when a same character in lower Case & Upper Case occurs in a string

I encounter this in Codility, so going to share my solution.

findBiggestAlphabet = function(S){
    console.log(`Processing..`, S)
    const char_arr = [...S];
    var max_char = "";
    for(var i=0; i<S.length-1;i++){
        var char = S[i];
        var char_c = char.toUpperCase()
        //console.log(`Processing ${char}`);
        var new_arr = char_arr.slice(i+1, char_arr.length);

        var exist_cap = new_arr.find(x=>x===char_c)? true: false;
        if (exist_cap){
            if (char_c>max_char){
                max_char = char_c;
            }
        }
    }
    if (max_char) return max_char;
    return "NO";
}

var A_1 = "aabcDBA";
var A_2 = "dabcDBA";
var A_3 = "abcde";
console.log(findBiggestAlphabet(A_1));
console.log(findBiggestAlphabet(A_2));
console.log(findBiggestAlphabet(A_3));
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
badgamerbad profile image
Vipul Dessai

why are you slicing the array, is it because you want to only iterate over the remaning elements from the index and you do not want to see the elements before the index?

if so how this algorithm will work for "ZyYz"