Lodash _.uniq() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Creates a duplicate-free version of an array, using SameValueZero for equality. Only the first occurrence of each element is kept. Order follows the original array. Since Lodash 0.1.0.

Pass the required arguments shown below. Lodash Array helpers usually return a new value — check whether the source array is mutated.

Syntax

Call _.uniq on an array. Start with this line.

Lodash
_.uniq(array)
CallWhat it does
_.uniq(array)Primary signature.
_.uniq(array)Call with an array argument.

Prefer import uniq from "lodash/uniq" when you want a smaller bundle.

Library declarations
Lodash
_.uniq(array)

Parameters

ParameterDescription
arrayRequired. Source to dedupe

Return Value

Return ValueDescription
*The value returned by _.uniq().

Lodash Examples

Example 1 — Official: Remove Duplicates

Keep the first of each value; drop later repeats.

Lodash
_.uniq([2, 1, 2]);
// → [2, 1]
Try It Yourself

How It Works

  1. First 2. Not seen yet → keep.
  2. 1. New value → keep.
  3. Second 2. Already seen → drop.
  4. Result. New array [2, 1]; source unchanged.

Example 2 — Unique Strings

Works the same for string values.

Lodash
_.uniq(["a", "b", "a", "c", "b"]);
// → ["a", "b", "c"]
Try It Yourself

How It Works

  1. Scan left to right. Record each new string.
  2. Repeats. Later "a" and "b" are skipped.
  3. Order. First-seen order: a, then b, then c.
  4. Case. "A" and "a" are different values.

Example 3 — NaN Collapses

SameValueZero treats every NaN as equal.

Lodash
_.uniq([1, NaN, 2, NaN]);
// → [1, NaN, 2]
Try It Yourself

How It Works

  1. First NaN. Kept when first seen.
  2. Second NaN. Matches the first → dropped.
  3. Numbers. 1 and 2 stay in place.
  4. Note. JSON.stringify prints NaN as null — the value is still NaN.

Example 4 — First-Seen Order

Uniq never re-sorts — it only drops later duplicates.

Lodash
_.uniq([3, 1, 2, 1, 3]);
// → [3, 1, 2]
Try It Yourself

How It Works

  1. Keep 3, 1, 2 as they first appear.
  2. Drop the later 1 and 3.
  3. Not sorted. Result is not [1, 2, 3].
  4. Need sorted + unique? Sort first, or use _.sortedUniq on sorted data.

Example 5 — Objects Compare by Reference

Equal-looking objects are unique unless they are the same reference.

Lodash
var a = { x: 1 };
_.uniq([a, { x: 1 }, a]);
// → [{ x: 1 }, { x: 1 }]   // two objects; last a dropped

_.uniqBy([{ x: 1 }, { x: 1 }], "x");
// → [{ x: 1 }]             // compare by property
Try It Yourself

How It Works

  1. uniq. Different object instances stay — only the repeated a ref is dropped.
  2. uniqBy. Same x → one object kept.
  3. Lesson. Prefer By/With for object identity by fields.
  4. Primitives. Numbers and strings are the sweet spot for plain uniq.

Browser & Runtime Support

_.uniq is a Lodash Array helper (since 0.1.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 0.1.0+

_.uniq()

Available in [email protected] full builds and the lodash/uniq modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.uniq Lodash 0.1.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import uniq from "lodash/uniq" for smaller builds. Native alternative: [...new Set(arr)] for primitives (NaN handling differs slightly in older engines).

Frequently Asked Questions

_.uniq(array) creates a duplicate-free version of an array using SameValueZero equality. Only the first occurrence of each element is kept. Order follows the original array.
No. It returns a new array. The source is unchanged.
No. Ordering mirrors the positions of the surviving first occurrences.
sortedUniq assumes sorted input and only collapses consecutive duplicates. uniq remembers every prior value across the whole array.
Yes. SameValueZero treats NaN as equal to NaN, so only the first NaN is kept.
Use import uniq from "lodash/uniq" or require("lodash/uniq") for tree-shaking. Full build: import _ from "lodash" then _.uniq.

Next: _.uniqBy()

Dedupe objects and values after an iteratee (property or function).

_.uniqBy() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful