Back to all solutions

#1796 - Second Largest Digit in a String

Problem Description

Given an alphanumeric string s, return the second largest numerical digit that appears in s, or -1 if it does not exist.

An alphanumeric string is a string consisting of lowercase English letters and digits.

Solution

/**
 * @param {string} s
 * @return {number}
 */
var secondHighest = function(s) {
  let largest = -1;
  let result = -1;

  for (const char of s) {
    if (/\d/.test(char)) {
      const digit = parseInt(char);
      if (digit > largest) {
        result = largest;
        largest = digit;
      } else if (digit < largest && digit > result) {
        result = digit;
      }
    }
  }

  return result;
};