Lodash _.sortedUniq() method

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

What you’ll learn

  • How _.sortedUniq(array) keeps the first element of every contiguous equal run.
  • Why it runs in linear time by leaning on sorted ordering.
  • When uniq or sorting first is the safer choice.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Knowing how _.sortedIndex()-style ordering works helps you validate inputs before trusting neighbor comparisons.

  • You understand shallow uniqueness—nested arrays or objects still dedupe only when references repeat adjacently.
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.sortedUniq is Lodash’s streamlined deduper when duplicates arrive back-to-back—perfect after sorting numeric IDs, timestamps, or enumerated ranks.

Linear scan

One forward pass compares each element only with its predecessor—no hash tables required.

Stable survivors

The earliest occurrence inside each duplicate plateau survives—downstream consumers keep deterministic ordering.

Fresh array

Inputs remain untouched; assign or chain further Lodash calls on the returned copy.

Syntax

javascript
_.sortedUniq(array)
  • array: values presumed ascending sorted so duplicates appear in contiguous blocks.
  • Returns: new array omitting repeated neighbors via SameValueZero checks.
1

Collapse consecutive duplicates

Each plateau collapses to its leading element while overall ascending order stays intact.

javascript
import sortedUniq from "lodash/sortedUniq";

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

Already unique

Distinct ascending sequences round-trip into a new array reference sharing identical ordering.

javascript
import sortedUniq from "lodash/sortedUniq";

const xs = [10, 20, 30];
const out = sortedUniq(xs);
// out → [10, 20, 30]; out !== xs
Try it Yourself
3

Unsorted danger

Values separated by smaller intermediaries survive independently—sort first when you need global uniqueness.

javascript
import sortedUniq from "lodash/sortedUniq";

sortedUniq([2, 1, 1, 2]);
// → [2, 1, 2] — duplicate 2 remains because runs were not contiguous
Try it Yourself

📋 _.sortedUniq vs _.uniq vs _.sortedUniqBy

APIInput expectationComplexity flavor
_.sortedUniq(array)Ascending sorted neighborsLinear single pass
_.uniq(array)Any orderTracks seen values globally
_.sortedUniqBy(array, iteratee)Sorted by projected keyLinear pass with iteratee projections

Pitfalls to avoid

Sort

Skipping sort

Partial uniqueness masks duplicated values that appear on both sides of unrelated entries.

Deep

Structural duplicates

Distinct object literals never compare equal—flatten ids first when semantics demand deduping rows.

NaN

Duplicate NaN runs

SameValueZero folds neighboring NaN placeholders exactly like other primitives.

❓ FAQ

No. Lodash builds and returns a new array containing one representative per consecutive equal run.
No. Callers must supply ascending sorted input when you rely on global uniqueness—otherwise only consecutive duplicates disappear.
Adjacent pairs use SameValueZero semantics consistent with other Lodash index helpers.
uniq tracks prior occurrences across the whole array and tolerates unsorted input. sortedUniq optimizes for sorted buffers by comparing only neighbors.
Use import sortedUniq from "lodash/sortedUniq" or require("lodash/sortedUniq") for tree-shaking friendly bundles.

Summary

Did you know?

Because sortedUniq only compares neighbors, it matches best right after a sort—or right before _.sortedIndexOf()-style analytics—when identical values already sit in contiguous blocks.

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