Activity Selection Implementation in JavaScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Activity Selection runs in O(n log n) time and O(1) space, in JavaScript as in every other language.
- ✓Array.prototype.sort((a, b) => a[1] - b[1]) is the entire dependency here, no separate priority-queue or heap type is needed, because activity selection only ever compares against the single most-recently-kept activity, not a frontier of candidates.
- ✓Always pass a comparator. Spread into a copy before sorting, [...activities].sort(...), rather than sorting the caller's array in place: sort() mutates and returns the same reference, so an un-copied sort is a hidden side effect on whatever array the caller handed you.
- ✓The JavaScript trap: A bare .sort() with no comparator converts every element to a string first, fine for words, wrong for anything numeric.
- ✓V8 has sorted with TimSort, guaranteeing stability, since V8 7.0 / Chrome 70 (2018); before that it used an unstable QuickSort for arrays with more than 10 elements (and insertion sort below that).
What Activity Selection actually is
Activity Selection in JavaScript: what you actually reach for
JavaScript implementation
function selectActivities(activities) {
const ordered = [...activities].sort((a, b) => a[1] - b[1]); // by finish, not start
const selected = [ordered[0]];
let lastFinish = ordered[0][1];
for (let i = 1; i < ordered.length; i += 1) {
const [start, finish] = ordered[i];
if (start >= lastFinish) {
selected.push(ordered[i]);
lastFinish = finish;
}
}
return selected;
}
const activities = [[1, 4], [3, 5], [0, 6], [5, 7], [8, 9], [5, 9]];
console.log(selectActivities(activities)); // [[1,4],[5,7],[8,9]]Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe JavaScript-specific trap
Performance in JavaScript, beyond the O(n log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which JavaScript type or library should I use for Activity Selection?
Array.prototype.sort((a, b) => a[1] - b[1]) is the entire dependency here, no separate priority-queue or heap type is needed, because activity selection only ever compares against the single most-recently-kept activity, not a frontier of candidates.
What is the most common JavaScript mistake when implementing Activity Selection?
A bare .sort() with no comparator converts every element to a string first, fine for words, wrong for anything numeric. [9, 80, 700].sort() returns [700, 80, 9], because the string '700' sorts before the string '80'. Forgetting the comparator on the finish-time sort here reproduces that exact bug: no error is thrown, the greedy pass just silently runs on the wrong order and returns a wrong-looking-right answer.
How does Activity Selection actually perform in JavaScript?
V8 has sorted with TimSort, guaranteeing stability, since V8 7.0 / Chrome 70 (2018); before that it used an unstable QuickSort for arrays with more than 10 elements (and insertion sort below that). TimSort's merge phase allocates a temporary array, so the sort step is O(n) space even though the greedy scan that follows it needs none. Time complexity is O(n log n) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? GitHub Copilot free tiers and academic plans are laid out on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.