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)];
"Smart" but Naïve Approach (Quadratic Time)
const uniqueArray = a.filter((item, pos) => a.indexOf(item) === pos);
Hashtables to the Rescue (Linear Time)
const uniq = (a) => { const seen = {}; return a.filter((item) => { return seen.hasOwnProperty(item) ? false : (seen[item] = true); }); };
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); } }); };
Sort and Filter (Linear Time)
const uniq = (a) => { return a.sort().filter((item, pos, ary) => !pos || item !== ary[pos - 1]); };
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); }) };
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(); };
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!