Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Vanilla JS: Remove duplicates using an Object like a Set</strong></p> <p>You can always try putting it into an object, and then iterating through its keys:</p> <pre><code>function remove_duplicates(arr) { var obj = {}; var ret_arr = []; for (var i = 0; i &lt; arr.length; i++) { obj[arr[i]] = true; } for (var key in obj) { ret_arr.push(key); } return ret_arr; } </code></pre> <p><strong>Vanilla JS: Remove duplicates by tracking already seen values (order-safe)</strong></p> <p>Or, for an order-safe version, use an object to store all previously seen values, and check values against it before before adding to an array.</p> <pre><code>function remove_duplicates_safe(arr) { var seen = {}; var ret_arr = []; for (var i = 0; i &lt; arr.length; i++) { if (!(arr[i] in seen)) { ret_arr.push(arr[i]); seen[arr[i]] = true; } } return ret_arr; } </code></pre> <p><strong>ECMAScript 6: Use the new Set data structure (order-safe)</strong></p> <p>ECMAScript 6 adds the new <code>Set</code> Data-Structure, which lets you store values of any type. <code>Set.values</code> returns elements in insertion order.</p> <pre><code>function remove_duplicates_es6(arr) { let s = new Set(arr); let it = s.values(); return Array.from(it); } </code></pre> <p><strong>Example usage:</strong></p> <pre><code>a = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"]; b = remove_duplicates(a); // b: // ["Adam", "Carl", "Jenny", "Matt", "Mike", "Nancy"] c = remove_duplicates_safe(a); // c: // ["Mike", "Matt", "Nancy", "Adam", "Jenny", "Carl"] d = remove_duplicates_es6(a); // d: // ["Mike", "Matt", "Nancy", "Adam", "Jenny", "Carl"] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload