Find if two arrays are repeated in array and then select them

arrays, javascript, jquery, multidimensional-array, object

Solution

You could take a `Map` with stringified arrays and count, then filter by count and restore the arrays.

var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]],
    result = Array
        .from(array.reduce(
            (map, array) =>
                (json => map.set(json, (map.get(json) || 0) + 1))
                (JSON.stringify(array)),
            new Map
         ))
        .filter(([, count]) => count > 2)
        .map(([json]) => JSON.parse(json));
        
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Filter with a map at wanted count.

var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]],
    result = array.filter(
        (map => a => 
            (json =>
                (count => map.set(json, count) && !(2 - count))
                (1 + map.get(json) || 1)
            )
            (JSON.stringify(a))
        )
        (new Map)
    );
        
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Unique!

var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]],
    result = array.filter(
        (s => a => (j => !s.has(j) && s.add(j))(JSON.stringify(a)))
        (new Set)
    );
        
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Problem

I have multiple arrays in a main/parent array like this: ``` var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]]; ``` here are the array's for simpler reading: ``` [1, 17] [1, 17] [1, 17] [2, 12] [5, 9] [2, 12] [6, 2] [2, 12] [2, 12] ``` I want to select the arrays that are repeated 3 or more times (> 3) and assign it to a variable. So in this example, `var repeatedArrays` would be `[1, 17]` and `[2, 12]`. So this should be the final result: ``` console.log(repeatedArrays); >>> [[1, 17], [2, 12]] ``` I found something similar here but it uses underscore.js and lodash. How could I it with javascript or even jquery (if need be)?

Original source

Related problems