Find the Most Competitive Subsequence LeetCode solution in JavaScript
1var mostCompetitive = function (nums, k) {2let nbToRemove = nums.length - k,3stack = [];4for (const num of nums) {5while (num < stack[stack.length - 1] && stack.length && nbToRemove) {6stack.pop();7nbToRemove--;8}9stack.push(num);10}11while (nbToRemove) {12stack.pop();13nbToRemove--;14}15return stack;16};