0

I am going to demonstrate the essence of the question. I think this should work.

const arr = [
    ["apple" , 3],
    ["banana", 8],
    ["apple", 3] 
    ];

const arr1 = ["apple", 3];

You can clearly see there is a duplicate element. But that may not be relevant at this point.

If I do the following:

isArr1Included = arr.includes(arr1);
// I expect the ouput: true

The output is false.

If I do this:

const foo = (arr[0] === arr1);

foo will be false.

So, how can I get valid results here? I come from a well rounded Python background, where I believe something like this would work properly, however I can't figure this out.

I understand that these are key-value pairs that I could use as a map instead of an array, however after some operation I will need to get back to using arrays, and I would like to avoid those extra steps of conversion.

My main goal here would be to remove the duplicate item.

Efficiency is key as these arrays can have thousands of elements(arrays)

EDIT:

Would this be faster than looping through all elements? (below)

Converting the key-value pairs within the array to strings (Array.prototype.toSting) and finding duplicate strings with . filter and . indexOf or perhaps .reduce

adeness
  • 1
  • 4
  • In JavaScript, `["apple", 3] !== ["apple", 3]`. See e.g. https://stackoverflow.com/q/7837456/3001761. – jonrsharpe May 11 '20 at 13:07
  • will the members of the array always be primitives? do you have a unique identifier for each item, maybe at some specific index? – Ayush Gupta May 11 '20 at 13:09
  • I have thought of such comparison. However I would like to do this faster and more efficiently. Going through an array with let's say 700 elements (each being array) and comparing those elements to find and delete duplicates...i would loop through the outer array 350 times if there is exactly one duplicate of each element and even more times if there are unique elements too. I thank you for pointing me to this answer and I will look further into it, but I am looking for a more efficient way here. Also I will Edit the question a bit. – adeness May 11 '20 at 13:25
  • Memebers are always primitives and i can guarantee that they are always a string with a length of maximum 7 and an integer, no unique identifiers. – adeness May 11 '20 at 13:27

0 Answers0