I have a collection of arrays that require deduplication. The process needs to ensure that each individual array contains only unique elements, and across the entire collection, no two arrays are identical in terms of their contents.
Dealing with the first part is straightforward - I can utilize the Set
object to remove duplicates from each inner array. To achieve this, given the matrix arrays
, I could use the following code snippet:
const sets : string[][] = arrays.map(arr=>[...new Set(arr)].sort());
This code will result in an array of unique sets. But now the challenge lies in turning this into a set of sets. For example, if sets=[[a, b],[c],[d, a],[c],[e]]
, I want setOfSets
to be equal to [[a, b],[c],[d,a],[e]]
.
Simply applying setOfSets = [...new Set(sets)];
won't work due to the fact that arrays are considered unequal by default even if they contain the same values but reside at different memory addresses. Is there a way to make the Set
object compare arrays based on their values or perhaps another approach to achieve the desired outcome?
Edit
Original matrix:
[[a, b, b],
[c,c],
[b,a],
[d,a],
[c,c],
[e,e]]
post generation and sorting of sets:
[[a,b],
[c],
[a,b],
[d,a],
[c],
[e]]
desired result:
[[a,b],
[c],
[d,a],
[e]]