Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Shorter Set by mortonfox
import assert from "assert";
function removeMinMax(data: Set, total: number): Set {
for (let i = 0; i < total; ++i) {
if (data.size) data.delete(Math.min(...data));
if (data.size) data.delete(Math.max(...data));
}
return data;
}
console.log("Example:");
console.log(removeMinMax(new Set([8, 9, 18, 7]), 1));
// These "asserts" are used for self-checking
assert.deepStrictEqual(
removeMinMax(new Set([8, 9, 18, 7]), 1),
new Set([8, 9])
);
assert.deepStrictEqual(removeMinMax(new Set([8, 9, 7]), 0), new Set([8, 9, 7]));
assert.deepStrictEqual(removeMinMax(new Set([8, 9, 7]), 2), new Set([]));
assert.deepStrictEqual(removeMinMax(new Set([1, 2, 7, 8, 9]), 2), new Set([7]));
assert.deepStrictEqual(removeMinMax(new Set([]), 1), new Set([]));
console.log("Coding complete? Click 'Check' to earn cool rewards!");
Nov. 26, 2021
Comments: