Skip to content

Instantly share code, notes, and snippets.

@manzaloros
Last active October 4, 2020 16:50
Show Gist options
  • Save manzaloros/32dd20e5e6899a5f55c6893ae9bf7b67 to your computer and use it in GitHub Desktop.
Save manzaloros/32dd20e5e6899a5f55c6893ae9bf7b67 to your computer and use it in GitHub Desktop.
removeCoveredIntervals
const removeCoveredIntervals = (intervals) => {
let coveredIntervalsCount = 0;
const compareSet = new Set();
for (let i = 0; i < intervals.length; i += 1) {
const current = intervals[i];
for (let j = 0; j < intervals.length; j += 1) {
if (j === i || compareSet.has((intervals[j].toString()))) {
continue;
}
const compare = intervals[j];
if (current[0] <= compare[0] && current[1] >= compare[1]) {
compareSet.add(compare.toString());
coveredIntervalsCount += 1;
}
}
}
return intervals.length - coveredIntervalsCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment