JSFiddle - React, Tailwind, and code Playground

by manchagnu

JavaScript

/**
Write a function that takes an integer, deletes one of two consecutive digits and returns the greatest of all results.
Input: 1993487443. It should output: 199348743 because deleting two 4s yields a greater result than deleting two 9s.
Input: 1100. It should output 110 because deleting two 0s yields a greater result than deleting two 1s.
Input: 1199. It should return 199 because deleting 1s yields a greater result than deleting two 9s.
*/


var input = 1993487443;
console.log(doMagic(input));


function doMagic(input) {
  var items = input.toString().split('');
  var results = [];

  items.reduce(function(data, current, index) {
    if (data[current] && items[index - 1] === current) {
      results.push(
        []
          .concat(items.slice(0, index - 1))
          .concat(items.slice(index))
      );
    }

    data[current] = true;
    return data;
  }, {});

  return results
    .map(function(item) {
      return item.join('');
    })
    .sort(function(a, b) {
      return a > b;
    })
    .pop();
}