Lodash _.sortedUniq() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.uniq, except it is designed and optimized for sorted arrays. It keeps one value from each consecutive equal run and returns a new array. Part of Lodash Array since 4.0.0 (stable — not deprecated). Warning: the array must already be sorted ascending when you need full uniqueness.

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

Syntax

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

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

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

Library declarations
Lodash
_.sortedUniq(array)

Parameters

ParameterDescription
arrayRequired. Sorted ascending collection to dedupe

Return Value

Return ValueDescription
*New array with consecutive duplicates removed

Lodash Examples

Example 1 — Official: Collapse Consecutive Duplicates

Keep one value from each adjacent equal run.

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

How It Works

  1. Sorted source. Values ascend: 1, 1, 2.
  2. First. Push 1 into the result.
  3. Skip. The second 1 equals the last kept value — drop it.
  4. Keep. 2 differs → push → [1, 2].

Example 2 — Longer Duplicate Runs

Any length of adjacent equals collapses to one value.

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

How It Works

  1. Plateaus. Runs of 1, 2, and 3 sit side by side.
  2. One each. Lodash keeps the first of every run.
  3. Result. [1, 2, 3] — order preserved.
  4. Use. Ideal after a sort when scores or ids form blocks.

Example 3 — sortedUniq vs uniq on Unsorted Data

Neighbor-only compare leaves scattered duplicates behind.

Lodash
var unsorted = [2, 1, 2];

console.log(_.sortedUniq(unsorted)); // [2, 1, 2]
console.log(_.uniq(unsorted));       // [2, 1]
Try It Yourself

How It Works

  1. Neighbors. 2 then 1 then 2 — no two equals sit next to each other.
  2. sortedUniq. Nothing collapses → still [2, 1, 2].
  3. uniq. Tracks all seen values → second 2 drops → [2, 1].
  4. Rule. Sort first, or use _.uniq, when order is not guaranteed.

Example 4 — Sorted Strings

Works the same for string values after alphabetical sort.

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

How It Works

  1. Sorted labels. Letters already ascend: a, a, b, c, c.
  2. Runs. Two as and two cs collapse.
  3. Keep. Single b stays as-is.
  4. Result. ["a", "b", "c"].

Example 5 — Empty Array & Already Unique

Edge inputs stay simple and predictable.

Lodash
console.log(_.sortedUniq([]));        // []
console.log(_.sortedUniq([1, 2, 3])); // [1, 2, 3]
Try It Yourself

How It Works

  1. Empty. No elements → empty result.
  2. Unique. Every neighbor differs → every value is kept.
  3. New array. Still returns a fresh array (not the same reference).
  4. Safe. Safe defaults for pipelines that may receive empty lists.

Browser & Runtime Support

_.sortedUniq is a Lodash 4.x Array helper (since 4.0.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native engines have Set / spread patterns, but not this sorted-optimized helper by name.

✓ Stable · Lodash 4.0.0+

_.sortedUniq()

Available in [email protected] full builds and the lodash/sortedUniq 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
_.sortedUniq Lodash 4.0.0+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import sortedUniq from "lodash/sortedUniq" for smaller builds. Not a native browser API — load Lodash first.

Frequently Asked Questions

_.sortedUniq(array) is like _.uniq, but optimized for sorted arrays. It keeps one value from each consecutive equal run and returns a new array.
No. Lodash builds and returns a new array. The source array stays unchanged.
No. You must pass an ascending sorted array when you need global uniqueness. On unsorted data it only removes adjacent duplicates.
uniq tracks every prior value across the whole array and works on unsorted input. sortedUniq only compares neighbors, so it is faster when the array is already sorted.
Adjacent pairs use SameValueZero semantics — the same rule Lodash uses for other uniqueness helpers (NaN equals NaN, 0 equals -0).
Use import sortedUniq from "lodash/sortedUniq" or require("lodash/sortedUniq") so bundlers can tree-shake unused Lodash code. Full build: import _ from "lodash" then _.sortedUniq.

Next: _.sortedUniqBy()

Collapse sorted consecutive values using a custom iteratee.

_.sortedUniqBy() 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.

10 people found this page helpful