Sort and remove duplicates from text
Removing duplicate items from a list of 100,000 entries using a naive "check every item against every other item" loop (O(n²) complexity) would freeze your browser.
Instead, modern JavaScript uses the Set data structure. A Set is a collection of unique values. By simply passing a massive array into new Set(array), the browser's optimized engine instantly drops all duplicates in O(n) time, operating thousands of times faster than a manual loop.
Standard alphabetical sorting in JavaScript (array.sort()) uses ASCII character codes. This means uppercase "Zebra" will be sorted before lowercase "apple", because capital Z (code 90) comes before lowercase a (code 97).
Furthermore, ASCII sorting completely breaks when handling accented characters like "é" or "ñ". Professional sorting tools use localeCompare() to sort strings alphabetically according to human language rules, completely ignoring case and handling accents correctly.
What is the most efficient way to remove all duplicates from a large JavaScript array?