Home > Web Front-end > JS Tutorial > How Can I Efficiently Remove Duplicate Values from JavaScript Arrays?

How Can I Efficiently Remove Duplicate Values from JavaScript Arrays?

DDD
Release: 2025-01-03 12:13:41
Original
639 people have browsed it

How Can I Efficiently Remove Duplicate Values from JavaScript Arrays?

Removing Duplicate Values from Arrays in JavaScript

Are you dealing with an array that may contain duplicates and need to create a new array with only the unique values? Here's a comprehensive guide with multiple approaches to solve this common issue.

TL;DR: Using ES6 Set and Spread Syntax

const uniq = [...new Set(array)];
Copy after login

"Smart" but Naïve Approach (Quadratic Time)

const uniqueArray = a.filter((item, pos) => a.indexOf(item) === pos);
Copy after login

Hashtables to the Rescue (Linear Time)

const uniq = (a) => {
  const seen = {};
  return a.filter((item) => {
    return seen.hasOwnProperty(item) ? false : (seen[item] = true);
  });
};
Copy after login

The Best of Both Worlds (Linear Time)

const uniq = (a) => {
  const prims = { boolean: {}, number: {}, string: {} }, objs = [];
  return a.filter((item) => {
    const type = typeof item;
    if (type in prims) {
      return prims[type].hasOwnProperty(item) ? false : (prims[type][item] = true);
    } else {
      return objs.indexOf(item) >= 0 ? false : objs.push(item);
    }
  });
};
Copy after login

Sort and Filter (Linear Time)

const uniq = (a) => {
  return a.sort().filter((item, pos, ary) => !pos || item !== ary[pos - 1]);
};
Copy after login

Unique by Key (Filtering Based on a Callback)

const uniqBy = (a, key) => {
  const seen = {};
  return a.filter((item) => {
    const k = key(item);
    return seen.hasOwnProperty(k) ? false : (seen[k] = true);
  })
};
Copy after login

Preserving the First or Last Occurrence (Using Set or Map)

const uniqByKeepFirst = (a, key) => {
  const seen = new Set();
  return a.filter((item) => {
    const k = key(item);
    return seen.has(k) ? false : seen.add(k);
  });
};

const uniqByKeepLast = (a, key) => {
  return [...new Map(a.map((x) => [key(x), x]))].values();
};
Copy after login

Choose the approach that aligns with your array type and performance requirements.

The above is the detailed content of How Can I Efficiently Remove Duplicate Values from JavaScript Arrays?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template