Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove duplicate arrays inside an array in Javascript?

How do I remove duplicate sorted arrays in Javascript? I have tried making a new array, using spread syntax on a new set, but that looks extremely complicated, and forced my result set to look like this:

[ [ '-', '1', ',', '0', ',', '1' ],
  [ '-', '1', ',', '-', '1', ',', '2' ] ]
[ [ '-', '1', ',', '0', ',', '1' ],
  [ '-', '1', ',', '-', '1', ',', '2' ] ]
[ [ '0', ',', '0', ',', '0' ] ]
[ [ '0', ',', '0', ',', '0' ] ]
[ [ '-', '4', ',', '-', '2', ',', '6' ],
  [ '-', '4', ',', '0', ',', '4' ],
  [ '-', '4', ',', '1', ',', '3' ],
  [ '-', '4', ',', '2', ',', '2' ],
  [ '-', '2', ',', '-', '2', ',', '4' ],
  [ '-', '2', ',', '0', ',', '2' ] ]
[ [ '-', '4', ',', '-', '2', ',', '6' ],
  [ '-', '4', ',', '0', ',', '4' ],
  [ '-', '4', ',', '1', ',', '3' ],
  [ '-', '4', ',', '2', ',', '2' ],
  [ '-', '2', ',', '-', '2', ',', '4' ],
  [ '-', '2', ',', '0', ',', '2' ] ]

or

[ [ '-1,0,1' ], [ '-1,-1,2' ] ]
[ [ '-1,0,1' ], [ '-1,-1,2' ] ]
[ [ '0,0,0' ] ]
[ [ '0,0,0' ] ]
[ [ '-4,-2,6' ],
  [ '-4,0,4' ],
  [ '-4,1,3' ],
  [ '-4,2,2' ],
  [ '-2,-2,4' ],
  [ '-2,0,2' ] ]
[ [ '-4,-2,6' ],
  [ '-4,0,4' ],
  [ '-4,1,3' ],
  [ '-4,2,2' ],
  [ '-2,-2,4' ],
  [ '-2,0,2' ] ]

mind you, these are after I set them to strings. The set will not remove duplicate sorted arrays since they are different objects.

Here is the code.

var threeSum = function (arr) {
    let map = new Map();
    for (let i = 0; i < arr.length; i++) {
        let num = arr[i];
        if (map.has(num)) {
            map.get(num).push(i);
        }
        else {
            map.set(num, [i]);
        }
    }

    const results = [];
    for (let i = 0; i < arr.length; i++) {
        for (let j = i + 1; j < arr.length; j++) {
            let diff = (arr[i] + arr[j]);
            diff = (diff == 0) ? diff: diff * -1;
            if (map.has(diff)) {
                let mArr = map.get(diff);
                for (let k = 0; k < mArr.length; k++) {
                    if (mArr[k] != i && mArr[k] != j ) {
                        results.push([arr[i], arr[j], diff]);
                    }
                }
            }
        }
    }

    results.forEach((value) => {
        value.sort((a, b) => { return a - b });
    });
    let newResults = results.filter((val, idx, arr) => {
        /// how do i do this
    })

    console.log(newResults);
    return newResults;

}

console.log(threeSum([-1, 0, 1, 2, -1, -4]));
console.log(threeSum([0,0,0,0]));
console.log(threeSum([-4,-2,-2,-2,0,1,2,2,2,3,3,4,4,6,6]));
like image 695
Tae Soo Kim Avatar asked May 05 '26 14:05

Tae Soo Kim


1 Answers

A set is perfect for this task. Sets maintain the condition that all of its elements are unique. This is how you can accomplish your task:

  1. Create a new set containing the elements of results: let set = new Set(results.map(result => JSON.stringify(result))
  2. Convert the set into an array. This can be done using Array.from(set).map(elem => JSON.parse(elem))
like image 188
chiragzq Avatar answered May 08 '26 04:05

chiragzq