JS Built-ins
TypeScriptbeginner

Generics β€” unique<T>

πŸ“– Usage Example
unique([1, 2, 2, 3, 3, 3])   // β†’ [1, 2, 3]
unique(['a', 'a', 'b'])       // β†’ ['a', 'b']

unique<T> removes duplicates by passing the array through a Set, preserving insertion order.

Generics: unique<T>

Challenge: Implement unique<T>(arr: T[]): T[] returning only the distinct values, preserving order.

unique([1, 2, 2, 3, 3])  // β†’ [1, 2, 3]
unique(['a', 'b', 'a'])  // β†’ ['a', 'b']
πŸ’‘ Hints (1)
  • β€’ Set preserves insertion order and removes duplicates.