Removing Duplicate Arrays from an Array of Arrays

In the Useful Array Tricks article, one of the tricks described how to ensure the contents of our array are unique and no duplicate items exist. The technique described really only worked when the array items in question were primitives like text or numbers. In this article, we will go one step further and look at another common case. What if the array items with potential duplicates were themselves arrays? How will we both identify the duplicate arrays and also remove those duplicates to ensure we have an array made up only of unique arrays? In this article, we’ll find out how!


This is a companion discussion topic for the original entry at https://www.kirupa.com/javascript/removing_duplicate_arrays_from_array.htm

Really enjoyed this article and didn’t even think about your approach when I saw your initial question. Glad you published the article and pointed out these additional methods.

1 Like

that is very useful

1 Like

This is so well explained .Thank you for the detailed explanation.
I have a list of stores with in an array.Each item in an array is an array with the store name and distance.I want to filter out the items that are closer to me.

Input: [[“Chipotle” , 0.2],
["Starbucks: , 0.3],
[“Safeway”, 0.4],
[“Safeway”, 0.2],
[“Starbucks”, 0.4],
].

OUTPUT should be: [[“Chipotle” , 0.2],
["Starbucks: , 0.3],
[“Safeway”, 0.2],
].
Any idea?

Hey Swasak! I will try to post a more detailed response, but the way to approach this is to:

  1. Sort by distance: https://www.kirupa.com/html5/array_tricks.htm#array_sorting
  2. Filter out the further away values by whatever criteria you come up with

From a performance point of view, filtering first and then sorting may be better! Not sure why I randomly thought of that right now :grinning:

Is there a way to do the opposite? Remove from parent and keep the duplicate in child array.

@Jerry_Seigle. Do you mean something like this?

let original = [1,2,2,3,3,3] 
let removed = removeDuplicates(original)
// original = [1,2,3]
// removed = [2,3,3]