1
/**
2
* @param {string} s
3
* @return {number}
4
*/
5
var secondHighest = function (s) {
6
//let's start by getting rid of all the non-digits
7
let myRegex = /\D/g;
8
s = s.replace(myRegex, "");
9
var digits = s.split("");
10
//at this point, digits is an array of all the digit characters that were originally in s
11
digits.sort(function (a, b) {
12
return b - a;
13
}); //sort the array into descending order
14
var res = new Set(digits); //turn the array into a set, to remove duplicates
15
if (res.size <= 1) {
16
//if the set is sized 1, then there is no second largest digit
17
return -1;
18
} else {
19
//otherwise, the second largest digit would be at index 1 (because we sorted the array into descending order)
20
return [...res][1];
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0