0

I have array of arrays, each array has unique items . I need to find and collect all duplicate items between all arrays to new array .

For this input : [[1,2,6,9],[3,2,7,5,12],[1,3]]

I need this output: [1,2,3]

Does anyone now what is the best way to do it ?

0

3 Answers 3

1

You could take a closure over an object for a count of the values.

const
    data = [[1, 2, 6, 9], [3, 2, 7, 5, 12], [1, 3]],
    result = data
        .flat()
        .filter(
            (o => v => (o[v] = (o[v] || 0) + 1) === 2)
            ({})
        );

console.log(result);

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

Comments

1

You can try this, optimal with O(3N)=== O(N) complexity

let arr = [
  [1, 2, 6, 9],
  [3, 2, 7, 5, 12],
  [1, 3]
];

let a = arr.flat()

let obj = {};

for (let i = 0; i < a.length; i++) {
  if (obj[a[i]]) obj[a[i]]++;
  else obj[a[i]] = 1
}
let repeated = [];
for (let i in obj) {
  if (obj[i] > 1) {

    repeated.push(i)
  }
}

console.log(repeated)

1 Comment

Thanks, is It possible to accumulate the array it came from , as key or object ? so the results would look something like this:{0_2:1,0_1:2,1_2:3)
0

Depending on what is meant by "duplicates" @Nina's solution can be modified to include only those cases where the value occurrs exactly twice. My slightly modified example snippet will exclude the value 1 as it occurs three times:

const data = [[1, 2, 6, 9], [3, 2, 7, 5, 12], [1, 3], [1, 7]], k=2, // no. of occurrences

result = data.reduce((o,c,i)=>(c.forEach(v=>o[v]=(o[v]||0)+1),o),[])
             .map((v,i,a)=>(a[i] = v===k ? i : undefined ))
             .filter(v=>v);

console.log(result);

caveat: This will only work with numeric values (integers!).

But here is another version that will work with any kind of content:

var data = [[1, 2, 6, 9, "c"], [3, "a", 2, 7, 5, 12], [1, "c", 3], [1, 7]], k=2;

res=[...data.reduce((o,c,i)=>(c.forEach(v=>o.set(v,(o.get(v)||0)+1)),o),
                    new Map()).entries()]
    .filter(([k,v])=>v===2)
    .map(([k,v])=>k)
   

console.log(res)

Update

If you want the addresses of the first location the duplicates were found you could do it like this:

var data = [[1, 2, 6, 9, "c"], [3, "a", 2, 7, 5, 12], [1, "c", 3], [1, 7]], n=2;

res=[...data.reduce((o,c,i)=>(c.forEach(v=>o.set(v,(o.get(v)||0)+1)),o),
                    new Map()).entries()]
    .filter(([k,v])=>v===n)
    .map(([k,v],j)=>[data.findIndex((d,i)=>(j=d.indexOf(k))>-1)+"_"+j, k])

console.log(Object.fromEntries(res))

2 Comments

thank you all , i wanted to know if it possible to store the place the duplicates came from, i mean first array second array etc. ?
So, you want the "address" of their first occurrence, yes, like in {"0_2":1,"0_1":2,"1_2":3) in your original example? Or, for my sample data, the result would be {"0_1":2,"0_4":"c","1_0":3,"1_3":7) - check my edited answer.

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.