Lodash _.uniq() method

Beginner
⏱️ 5 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.uniq(array) strips duplicates without reordering survivors.
  • Why SameValueZero differs from naive === scans around edge cases.
  • When sortedUniq, _.uniqBy(), or native Set swaps make sense.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Knowing how _.union() merges multiple arrays clarifies that uniq operates on just one collection at a time.

  • You understand shallow uniqueness—distinct object literals never dedupe unless values compare equal.
  • You can open Try-it labs or run snippets locally.

Overview

_.uniq is the quickest Lodash helper when you only need a duplicate-free snapshot—telemetry batches, tag clouds, or playlist cleanup.

First wins

Earlier duplicates disappear silently while survivors keep their relative spacing.

Spec-aligned equality

SameValueZero keeps Lodash consistent with sibling helpers such as includes.

Immutable source

Inputs remain untouched for downstream consumers expecting the noisy original.

Syntax

javascript
_.uniq(array)
  • array: collection to inspect (array-like values coerce predictably).
  • Returns: new array containing unique elements ordered by first appearance.
1

Collapse scattered repeats

Duplicates may be non-adjacent—uniq still removes later copies.

javascript
import uniq from "lodash/uniq";

uniq([2, 1, 2, 3, 1]);
// → [2, 1, 3]
Try it Yourself
2

Ordering stays intuitive

Survivors surface in the same relative order they originally appeared—only duplicates vanish.

javascript
import uniq from "lodash/uniq";

uniq(["delta", "alpha", "bravo", "alpha", "charlie"]);
// → ["delta", "alpha", "bravo", "charlie"]
Try it Yourself
3

Types stay distinct

1 and "1" remain separate slots because SameValueZero distinguishes types.

javascript
import uniq from "lodash/uniq";

uniq([1, "1", 1, "1", true]);
// → [1, "1", true]
Try it Yourself

📋 _.uniq vs _.sortedUniq, _.uniqBy, native Set

APIStrengthTrade-off
_.uniq(array)Global dedupe with stable orderingTracks every prior value—costlier than neighbor-only scans
_.sortedUniq(array)Linear pass on sorted inputRequires sorted / contiguous duplicates
_.uniqBy(array, iteratee)Keyed uniquenessNeeds iteratee when duplicates differ visually
[...new Set(array)]Built-in primitive dedupeNo lodash array-like helpers or iteratee pipeline parity

Pitfalls to avoid

Obj

Reference identity

Different object literals never dedupe—project ids via uniqBy instead.

Sort

Expecting sorted output

uniq does not sort; call sort separately when lexical order matters.

JSON

NaN display

Logging via JSON.stringify masks NaN—inspect live arrays in DevTools when debugging.

❓ FAQ

No. Lodash returns a brand-new array containing unique elements in first-seen order.
SameValueZero—matching other Lodash uniq-family helpers (NaN collapses with NaN, etc.).
No. Ordering mirrors the original positions of surviving elements.
sortedUniq assumes ascending sorted input and collapses only consecutive duplicates. uniq walks the entire array and remembers prior values.
Use import uniq from "lodash/uniq" or require("lodash/uniq") for tree-shaken bundles.

Summary

Did you know?

Need linear scans on already sorted data? Switch to _.sortedUniq()—it only compares neighbors, whereas plain uniq remembers every prior value. When duplicates share a stable field such as id, graduate to _.uniqBy().

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.

6 people found this page helpful