The constraint takes away hash lookups, so first clarify what's allowed (arrays are fine; is a Map banned too?). The direct answer is messages.filter((m, i) => messages.indexOf(m) === i): it keeps the first copy in order but makes O(n²) comparisons. The better answer sorts. Sort the positions by message. sort is stable, so equal messages sit together with the earliest first. Mark the first of each run in a boolean array, then filter the original list. That's O(n log n) and keeps the original order. For real messages, dedupe by a key (an id, or normalised text) and choose whether the first or the latest copy wins.
Deduplicate a list of messages without using an object or a Set.
Answer it out loud, in your own words. Then compare.
Interviewers listen for how you get there, not only where you land. The answer opens once you have tried.
A message is kept if indexOf finds it at its own position, which means it hasn't appeared earlier. It's one line and keeps order, but indexOf scans the array for every element: O(n²), fine for a chat window and too slow for a big log.
// Keep a message only if this is its first position. Arrays only; O(n²) comparisons.function dedupe(messages) {return messages.filter((msg, i) => messages.indexOf(msg) === i);}
passes 3 checks run on every build, against the native method where there is one
- keeps the first of each, in order
- empty and single
- case and spaces count as different here
Sorting the messages themselves would lose their order, so sort an array of positions by message. Array.prototype.sort has been required to be stable since ES2019, so equal messages end up adjacent in their original order, earliest first. One pass marks the first of each run, and a final filter returns them in the original order. That's O(n log n) time and O(n) space with only arrays. It's checked against a Set-based answer on 300 random lists and on 100,000 messages.
// O(n log n) without hashing: sort the positions by message. Sort is stable, so equal messages// sit together with the earliest first; mark those, then filter in original order.function dedupe(messages) {const order = messages.map((_, i) => i);order.sort((a, b) => (messages[a] < messages[b] ? -1 : messages[a] > messages[b] ? 1 : 0));const keep = new Array(messages.length).fill(false);for (let j = 0; j < order.length; j++) {if (j === 0 || messages[order[j]] !== messages[order[j - 1]]) keep[order[j]] = true;}return messages.filter((_, i) => keep[i]);}
passes 3 checks run on every build, against the native method where there is one
- same result as a Set on 300 random lists
- fast enough for 100,000 messages
- keeps the earliest position, not the sorted order
Messages are usually objects, so compute a key per item (String(m.id), or normalised text) and run the same sort over keys. For each run of equal keys, keep the first or the last position, so an edited message replaces the original. Normalising (Unicode NFC, trimmed, collapsed spaces, lower case) makes 'Hello world' and ' hello world' one message, and composed and decomposed é too. Both modes are checked against a Map-based version.
// Real messages are objects: dedupe by a key (an id, or normalised text), keep the first or the// latest copy, still without a Set or a lookup object, and still in the original order.function dedupeBy(items, key = (x) => x, { keep = 'first' } = {}) {const keys = items.map(key);const order = items.map((_, i) => i);order.sort((a, b) => (keys[a] < keys[b] ? -1 : keys[a] > keys[b] ? 1 : 0)); // stable: equal keys keep their orderconst chosen = new Array(items.length).fill(false);for (let j = 0; j < order.length; ) {let end = j;while (end + 1 < order.length && keys[order[end + 1]] === keys[order[j]]) end++; // one run of equal keyschosen[keep === 'last' ? order[end] : order[j]] = true;j = end + 1;}return items.filter((_, i) => chosen[i]);}// "Hello world" and "hello world" are the same message to a person.const normalise = (text) => text.normalize('NFC').trim().replace(/\s+/g, ' ').toLowerCase();
passes 3 checks run on every build, against the native method where there is one
- by id, keeping the latest edit
- normalised text treats spacing, case and Unicode forms as one
- matches a Map-based answer on 300 random cases, both modes
Chat and notification feeds
Reconnecting clients often receive messages they already have; feeds dedupe by message id and keep the latest edit.
Sort-based dedupe is how databases do DISTINCT
Without enough memory for a hash table, databases sort and skip adjacent duplicates, the same idea as the intermediate stage.
Unicode normalisation
The same visible text can be different code points (é as one character or e plus an accent); normalize('NFC') makes them equal before comparing.
Sorting the messages themselves, which loses the original order.
Relying on sort stability in an old engine: before ES2019 it wasn't guaranteed, so add a tie-break on position if you must support one.
Using a
Mapafter being told "no object" without asking: clarify the constraint first.Comparing raw text when users see
Hiandhias the same message.
Why might an interviewer ban objects and Sets?
To see whether you know alternatives to hashing: sorting (O(n log n)), or O(n²) scans, and when each is acceptable.
What if the list doesn't fit in memory?
External sort: sort chunks, write them out, merge them while skipping adjacent duplicates. Or hash into buckets on disk.
What about messages that are almost the same?
Near-duplicate detection needs a similarity measure (edit distance, shingles with MinHash) and a threshold, not equality.