Create an array without duplicates using ES6 Set
npx @tessl/cli install tessl/npm-array-uniq@3.0.0Array Uniq is a lightweight JavaScript utility that creates arrays without duplicates by leveraging ES6 Set functionality. It provides a simple, zero-dependency solution for deduplicating arrays of any type while preserving the order of first occurrences.
npm install array-uniqimport arrayUniq from 'array-uniq';For CommonJS (Node.js environments without ES module support):
const { default: arrayUniq } = require('array-uniq');import arrayUniq from 'array-uniq';
// Remove duplicates from numbers
const numbers = arrayUniq([1, 1, 2, 3, 3]);
// Result: [1, 2, 3]
// Remove duplicates from strings
const words = arrayUniq(['foo', 'foo', 'bar', 'foo']);
// Result: ['foo', 'bar']
// Works with any data type
const mixed = arrayUniq([1, '1', 1, 'foo', 'foo', true, true]);
// Result: [1, '1', 'foo', true]Creates a new array with duplicate values removed, preserving the order of first occurrences.
/**
* Create an array without duplicates
* @param array - The array to remove duplicates from
* @returns New array with duplicates removed
*/
function arrayUniq<ValueType>(array: readonly ValueType[]): ValueType[];Implementation Details:
Set for O(n) deduplication performanceType Safety:
Usage Examples:
import arrayUniq from 'array-uniq';
// Basic number deduplication
const numbers = arrayUniq([1, 2, 2, 3, 1, 2, 4]);
console.log(numbers); // [1, 2, 3, 4]
// String deduplication
const strings = arrayUniq(['a', 'a', 'b', 'a', 'c', 'a', 'd']);
console.log(strings); // ['a', 'b', 'c', 'd']
// Mixed types
const mixed = arrayUniq([1, '1', 2, '2', 1, '1']);
console.log(mixed); // [1, '1', 2, '2']
// Object references (compares by reference)
const obj1 = { id: 1 };
const obj2 = { id: 2 };
const objects = arrayUniq([obj1, obj2, obj1]);
console.log(objects); // [{ id: 1 }, { id: 2 }]
// Empty array handling
const empty = arrayUniq([]);
console.log(empty); // []Performance Characteristics: