Back to the challenge/src/normalize-tags.ts
Commented solution
Normalize and dedupe tags
Why it works
- One pass: normalize each tag, skip it if empty or already seen, otherwise keep it.
- The Set is only the memory of what was kept; the result array preserves the order.
- Sorting the Set would pass most tests and break the order requirement, a common trap.
- O(n) time and O(n) extra space. The input array is only read.
export function normalizeTags(input: string[]): string[] {const seen = new Set<string>();const result: string[] = [];for (const raw of input) {const tag = raw.trim().toLowerCase().replace(/\s+/g, "-");if (tag === "" || seen.has(tag)) continue;seen.add(tag);result.push(tag);}return result;}
What a good explanation covers
- Explains why normalization happens before the duplicate check.
- Mentions the cost of the approach (time and memory).
- Says how the input stays untouched.