0

My input is

let data  = [
       [1,2,3],
       [1,3,2,4],
       [3,2,1,5],
       [1,2,3],
       [3,2,1]
    ];

after this peace of code:

var dataUnique = data.reduce(function (out, item) {
    return out.concat(out.filter(function (comp) {
      return item.toString() == comp.toString();
    }).length ? [] : [item])
  }, []);
  console.log(data, dataUnique);

Output give me array of 4 element

   [1,2,3],
   [1,3,2,4],
   [3,2,1,5],
   [3,2,1]

but expected output would be

 [1,2,3],
   [1,3,2,4],
   [3,2,1,5]

Can anyone suggest any solution. Thanks.

8
  • What determines your expected output? Can you explain? Commented Apr 29, 2018 at 1:03
  • expected output would be an array that contain only unique list of array. [1,2,3] and [3,2,1] both contain same element with different index. when I tried, this two indicate as different array but expected output should be recognize both as same. I am thinking about another for loop. but not sure. Commented Apr 29, 2018 at 1:07
  • 1
    I don't understand, isn't the single [4] pretty unique? Please elaborate on what exactly the condition is, the failing code alone isn't very informative (after all, it's failing) Commented Apr 29, 2018 at 1:10
  • expected output would be an array that contain only unique list of array. [1,2,3] and [3,2,1] both contain same element with different index. when I tried, this two indicate as different array but expected output should be recognize both as same. I am thinking about another for loop. but not sure. Commented Apr 29, 2018 at 1:11
  • 1
    The [4] matches your condition though - it's unique, so why would you manually splice it? There must be something significant here Commented Apr 29, 2018 at 1:16

2 Answers 2

1

You can create some sort of hash — on object, Map, Set, etc and use a stringified version of your input as keys. Here's an example using a Set:

let data  = [
    [1,2,3],
    [1,3,2,4],
    [3,2,1,5],
    [1,2,3],
    [3,2,1]
 ];

let set = new Set()
let result = data.reduce((a, i) => {
    let k = i.concat().sort().join('_')
    if (!set.has(k)) {
        set.add(k)
        a.push(i)
    }
    return a
}, [])

console.log(result)

This could be a little simpler if you didn't mind the output having sorted versions of your input.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the solution. quit idle for what I want.
if you have time please add some descriptive comment on your code. it will better for understanding for me and other user who will have same problem.
0

This is an alternative using the functions reduce, every and includes.

Basically, this approach checks if one number doesn't exist within the previously checked arrays.

let data = [  [1, 2, 3],  [1, 3, 2, 4],  [3, 2, 1, 5],  [1, 2, 3],  [3, 2, 1]],
    result = data.reduce((a, c) => {
      c.forEach(n => {
        if (a.length == 0 || !a.every(arr => arr.includes(n))) a.push(c);
      });
      return a;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.