I am trying to get the key that has maximum value in the map which I have created through new Map() and added the keys & values in it.
Now, I wanted to get the key whose value is maximum and if two keys have the same values then I want to return the lexicographically largest.
For eg: {'a': 20, 'b':20} then I want b to be return.
my code:
var slowestKey = function(releaseTimes, keysPressed) {
let map=new Map();
map.set(keysPressed[0],releaseTimes[0]);
for(let i=0; i<releaseTimes.length-1; i++){
let time=releaseTimes[i+1]-releaseTimes[i];
map.set(keysPressed[i+1],time);
}
let max= Math.max(...map.values());
console.log(map)
console.log(Math.max(...map.values()));
};
Input:
Input: releaseTimes = [9,29,49,50], keysPressed = "cbcd"
Expected Output: "c"
console.log:
Map(3) { 'c' => 20, 'b' => 20, 'd' => 1 }
20
How can I get the key whose value is maximum and lexicographically bigger?