CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-array-uniq

Create an array without duplicates using ES6 Set

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Array Uniq

Array 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.

Package Information

  • Package Name: array-uniq
  • Package Type: npm
  • Language: JavaScript (with TypeScript definitions)
  • Installation: npm install array-uniq

Core Imports

import arrayUniq from 'array-uniq';

For CommonJS (Node.js environments without ES module support):

const { default: arrayUniq } = require('array-uniq');

Basic Usage

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]

Capabilities

Array Deduplication

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:

  • Uses native JavaScript Set for O(n) deduplication performance
  • Preserves order of first occurrence of each element
  • Supports any data type (numbers, strings, objects, booleans, etc.)
  • Returns a new array without modifying the original
  • Zero dependencies
  • Requires Node.js 12+

Type Safety:

  • Full TypeScript support with generic type preservation
  • Input array type is preserved in output
  • Supports readonly arrays as input

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:

  • Time complexity: O(n) where n is the length of input array
  • Space complexity: O(n) for the Set and output array
  • Memory efficient for large arrays with many duplicates
  • Faster than manual loop-based deduplication approaches
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/array-uniq@3.0.x
Publish Source
CLI
Badge
tessl/npm-array-uniq badge