Creates a duplicate-free version of arrays using SameValueZero for equality comparisons, with optimization support for sorted arrays and custom iteratee functions.
npx @tessl/cli install tessl/npm-lodash--uniq@3.2.0Lodash Uniq provides array deduplication functionality, creating duplicate-free versions of arrays using SameValueZero for equality comparisons. It retains only the first occurrence of each element and offers optimization for sorted arrays with custom comparison logic through iteratee functions. The function is also available under the alias unique.
npm install lodash.uniqvar uniq = require('lodash.uniq');For ES modules (if supported):
import uniq from 'lodash.uniq';var uniq = require('lodash.uniq');
// Basic deduplication
uniq([2, 1, 2]);
// => [2, 1]
// Sorted array optimization
uniq([1, 1, 2], true);
// => [1, 2]
// Custom comparison with iteratee
uniq([1, 2.5, 1.5, 2], function(n) {
return Math.floor(n);
});
// => [1, 2.5]
// Property-based deduplication
uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]Creates a duplicate-free version of an array, using SameValueZero for equality comparisons, keeping only the first occurrence of each element.
/**
* Creates a duplicate-free version of an array, using SameValueZero
* for equality comparisons, in which only the first occurence of each element
* is kept. Providing `true` for `isSorted` performs a faster search algorithm
* for sorted arrays. If an iteratee function is provided it's invoked for
* each element in the array to generate the criterion by which uniqueness
* is computed. The `iteratee` is bound to `thisArg` and invoked with three
* arguments: (value, index, array).
*
* If a property name is provided for `iteratee` the created `_.property`
* style callback returns the property value of the given element.
*
* If a value is also provided for `thisArg` the created `_.matchesProperty`
* style callback returns `true` for elements that have a matching property
* value, else `false`.
*
* If an object is provided for `iteratee` the created `_.matches` style
* callback returns `true` for elements that have the properties of the given
* object, else `false`.
*
* @alias unique
* @param {Array} array The array to inspect.
* @param {boolean} [isSorted] Specify the array is sorted.
* @param {Function|Object|string} [iteratee] The function invoked per iteration.
* @param {*} [thisArg] The `this` binding of `iteratee`.
* @returns {Array} Returns the new duplicate-value-free array.
*/
function uniq(array, isSorted, iteratee, thisArg);
// Available as alias
var unique = uniq;Usage Examples:
Basic Deduplication:
// Remove duplicates from unsorted array
uniq([2, 1, 2]);
// => [2, 1]
uniq([1, 3, 2, 1, 3]);
// => [1, 3, 2]Sorted Array Optimization:
// Use isSorted flag for performance optimization
uniq([1, 1, 2], true);
// => [1, 2]
uniq([1, 2, 2, 3, 3, 3], true);
// => [1, 2, 3]Custom Iteratee Functions:
// Custom comparison logic
uniq([1, 2.5, 1.5, 2], function(n) {
return Math.floor(n);
});
// => [1, 2.5]
// With thisArg binding
uniq([1, 2.5, 1.5, 2], function(n) {
return this.floor(n);
}, Math);
// => [1, 2.5]Property Name Shorthand:
// Deduplicate by object property
uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]
uniq([{ name: 'Alice' }, { name: 'Bob' }, { name: 'Alice' }], 'name');
// => [{ name: 'Alice' }, { name: 'Bob' }]Object Pattern Matching:
// Deduplicate by object pattern match
uniq([
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': true },
{ 'user': 'pebbles', 'active': false }
], { 'active': false });
// => [{ 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': true }]Property-Value Matching:
// Deduplicate using property-value pair matching
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': true },
{ 'user': 'pebbles', 'active': false }
];
// Uses _.matchesProperty style callback
uniq(users, 'active', false);
// => [{ 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': true }][] when input is null, undefined, or has no lengthisIterateeCall to detect when parameters are passed in shorthand formsortedUniq optimized algorithm when isSorted is true