| 437 | } |
| 438 | |
| 439 | function sort_and_uniq(a: string[]) { |
| 440 | return a |
| 441 | .sort((a, b) => a.localeCompare(b)) |
| 442 | .reduce((accumulator: string[], currentValue: string) => { |
| 443 | const len = accumulator.length; |
| 444 | // If accumulator is empty or its last element != currentValue |
| 445 | // Since array is already sorted, elements with the same value |
| 446 | // are grouped together to be continugous in space. |
| 447 | // |
| 448 | // If currentValue != last element, then it must be unique. |
| 449 | if (len == 0 || accumulator[len - 1].localeCompare(currentValue) != 0) { |
| 450 | accumulator.push(currentValue); |
| 451 | } |
| 452 | return accumulator; |
| 453 | }, []); |
| 454 | } |