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












3















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)?










share|improve this question























  • You could compare them against their JSON values, however, [1, 17] wouldn't match [17, 1]

    – Get Off My Lawn
    Nov 23 '18 at 21:03













  • Choose best answer for you and accept it by click on gray "check" button on its left side

    – Kamil Kiełczewski
    Nov 24 '18 at 12:37
















3















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)?










share|improve this question























  • You could compare them against their JSON values, however, [1, 17] wouldn't match [17, 1]

    – Get Off My Lawn
    Nov 23 '18 at 21:03













  • Choose best answer for you and accept it by click on gray "check" button on its left side

    – Kamil Kiełczewski
    Nov 24 '18 at 12:37














3












3








3


1






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)?










share|improve this question














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)?







javascript jquery arrays object multidimensional-array






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 20:59









Timmy BalkTimmy Balk

366




366













  • You could compare them against their JSON values, however, [1, 17] wouldn't match [17, 1]

    – Get Off My Lawn
    Nov 23 '18 at 21:03













  • Choose best answer for you and accept it by click on gray "check" button on its left side

    – Kamil Kiełczewski
    Nov 24 '18 at 12:37



















  • You could compare them against their JSON values, however, [1, 17] wouldn't match [17, 1]

    – Get Off My Lawn
    Nov 23 '18 at 21:03













  • Choose best answer for you and accept it by click on gray "check" button on its left side

    – Kamil Kiełczewski
    Nov 24 '18 at 12:37

















You could compare them against their JSON values, however, [1, 17] wouldn't match [17, 1]

– Get Off My Lawn
Nov 23 '18 at 21:03







You could compare them against their JSON values, however, [1, 17] wouldn't match [17, 1]

– Get Off My Lawn
Nov 23 '18 at 21:03















Choose best answer for you and accept it by click on gray "check" button on its left side

– Kamil Kiełczewski
Nov 24 '18 at 12:37





Choose best answer for you and accept it by click on gray "check" button on its left side

– Kamil Kiełczewski
Nov 24 '18 at 12:37












6 Answers
6






active

oldest

votes


















9














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; }








share|improve this answer


























  • Awesome! This works! Is there any way to remove the duplicated arrays from the original array? So the final output of var array would be [[1, 17], [2, 12], [5, 9], [6, 2]]. I tried filter() and indexOf() but it did not work

    – Timmy Balk
    Nov 23 '18 at 21:46











  • for that task, you may use a Set instead of a Map.

    – Nina Scholz
    Nov 23 '18 at 21:55











  • Could you please edit your answer and show me? Do I use has()?

    – Timmy Balk
    Nov 23 '18 at 21:59











  • @TimmyBalk just remove .filter(([, count]) => count > 2) line - and don't change anything else

    – Kamil Kiełczewski
    Nov 23 '18 at 22:00













  • @KamilKiełczewski Excellent! Thank you so much. I'm going to research into the filter() function now, thanks again Nina and Kamil. But i'm assuming removing the .filter line is not the same as what Nina was saying?

    – Timmy Balk
    Nov 23 '18 at 22:02





















5














You can use Object.reduce, Object.entries for this like below






var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];


let res = Object.entries(
array.reduce((o, d) => {
let key = d.join('-')
o[key] = (o[key] || 0) + 1

return o
}, {}))
.flatMap(([k, v]) => v > 2 ? [k.split('-').map(Number)] : )


console.log(res)





OR may be just with Array.filters






var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

let temp = {}
let res = array.filter(d => {
let key = d.join('-')
temp[key] = (temp[key] || 0) + 1

return temp[key] == 3
})

console.log(res)








share|improve this answer





















  • 1





    First snipped don't meet requirements (return elements are strings not nubmers) - but the second snipped is brilliant, +1

    – Kamil Kiełczewski
    Nov 23 '18 at 21:58













  • Thank you @KamilKiełczewski. Updated first snippet to return Numbers.

    – Nitish Narang
    Nov 24 '18 at 4:19



















5














Try this



array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))


Time complexity O(n) (one array pass by filter function). Inspired by Nitish answer.



Explanation



The (r={}, a=>...) will return last expression after comma (which is a=>...) (e.g. (5,6)==6). In r={} we set once temporary object where we will store unique keys. In filter function a=>... in a we have current array element . In r[a] JS implicity cast a to string (e.g 1,17). Then in !(2-(r[a]=++r[a]|0)) we increase counter of occurrence element a and return true (as filter function value) if element a occurred 3 times. If r[a] is undefined the ++r[a] returns NaN, and further NaN|0=0 (also number|0=number). The r[a]= initialise first counter value, if we omit it the ++ will only set NaN to r[a] which is non-incrementable (so we need to put zero at init). If we remove 2- as result we get input array without duplicates - or alternatively we can also get this by a=>!(r[a]=a in r). If we change 2- to 1- we get array with duplicates only.






var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

var r= array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))

console.log(JSON.stringify(r));








share|improve this answer

































    3














    For a different take, you can first sort your list, then loop through once and pull out the elements that meet your requirement. This will probably be faster than stringifying keys from the array even with the sort:






    var arr = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]]
    arr.sort((a, b) => a[0] - b[0] || a[1] - b[1])

    // define equal for array
    const equal = (arr1, arr2) => arr1.every((n, j) => n === arr2[j])

    let GROUP_SIZE = 3
    first = 0, last = 1, res =

    while(last < arr.length){
    if (equal(arr[first], arr[last])) last++
    else {
    if (last - first >= GROUP_SIZE) res.push(arr[first])
    first = last
    }
    }
    if (last - first >= GROUP_SIZE) res.push(arr[first])
    console.log(res)








    share|improve this answer
























    • Interesting approach, but is it really faster?

      – Timmy Balk
      Nov 23 '18 at 21:52






    • 1





      @TimmyBalk this isn't a criticism of the JSON approach -- I think it's good, but if my test is correct, it is slower (at least on my browser). Here's a jsperf test: jsperf.com/2json-v-sort-loop

      – Mark Meyer
      Nov 23 '18 at 21:55





















    1














    ES6:



    const repeatMap = {}

    array.forEach(arr => {
    const key = JSON.stringify(arr)
    if (repeatMap[key]) {
    repeatMap[key]++
    } else {
    repeatMap[key] = 1
    }
    })

    const repeatedArrays = Object.keys(repeatMap)
    .filter(key => repeatMap[key] >= 3)
    .map(key => JSON.parse(key))





    share|improve this answer































      1














      You could also do this with a single Array.reduce where you would only push to a result property if the length is equal to 3:






      var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

      console.log(array.reduce((r,c) => {
      let key = c.join('-')
      r[key] = (r[key] || 0) + 1
      r[key] == 3 ? r.result.push(c) : 0 // if we have a hit push to result
      return r
      }, { result: }).result) // print the result property








      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452875%2ffind-if-two-arrays-are-repeated-in-array-and-then-select-them%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        9














        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; }








        share|improve this answer


























        • Awesome! This works! Is there any way to remove the duplicated arrays from the original array? So the final output of var array would be [[1, 17], [2, 12], [5, 9], [6, 2]]. I tried filter() and indexOf() but it did not work

          – Timmy Balk
          Nov 23 '18 at 21:46











        • for that task, you may use a Set instead of a Map.

          – Nina Scholz
          Nov 23 '18 at 21:55











        • Could you please edit your answer and show me? Do I use has()?

          – Timmy Balk
          Nov 23 '18 at 21:59











        • @TimmyBalk just remove .filter(([, count]) => count > 2) line - and don't change anything else

          – Kamil Kiełczewski
          Nov 23 '18 at 22:00













        • @KamilKiełczewski Excellent! Thank you so much. I'm going to research into the filter() function now, thanks again Nina and Kamil. But i'm assuming removing the .filter line is not the same as what Nina was saying?

          – Timmy Balk
          Nov 23 '18 at 22:02


















        9














        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; }








        share|improve this answer


























        • Awesome! This works! Is there any way to remove the duplicated arrays from the original array? So the final output of var array would be [[1, 17], [2, 12], [5, 9], [6, 2]]. I tried filter() and indexOf() but it did not work

          – Timmy Balk
          Nov 23 '18 at 21:46











        • for that task, you may use a Set instead of a Map.

          – Nina Scholz
          Nov 23 '18 at 21:55











        • Could you please edit your answer and show me? Do I use has()?

          – Timmy Balk
          Nov 23 '18 at 21:59











        • @TimmyBalk just remove .filter(([, count]) => count > 2) line - and don't change anything else

          – Kamil Kiełczewski
          Nov 23 '18 at 22:00













        • @KamilKiełczewski Excellent! Thank you so much. I'm going to research into the filter() function now, thanks again Nina and Kamil. But i'm assuming removing the .filter line is not the same as what Nina was saying?

          – Timmy Balk
          Nov 23 '18 at 22:02
















        9












        9








        9







        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; }








        share|improve this answer















        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; }








        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; }





        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; }





        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; }





        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; }





        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; }





        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; }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 27 '18 at 8:39

























        answered Nov 23 '18 at 21:06









        Nina ScholzNina Scholz

        186k1596170




        186k1596170













        • Awesome! This works! Is there any way to remove the duplicated arrays from the original array? So the final output of var array would be [[1, 17], [2, 12], [5, 9], [6, 2]]. I tried filter() and indexOf() but it did not work

          – Timmy Balk
          Nov 23 '18 at 21:46











        • for that task, you may use a Set instead of a Map.

          – Nina Scholz
          Nov 23 '18 at 21:55











        • Could you please edit your answer and show me? Do I use has()?

          – Timmy Balk
          Nov 23 '18 at 21:59











        • @TimmyBalk just remove .filter(([, count]) => count > 2) line - and don't change anything else

          – Kamil Kiełczewski
          Nov 23 '18 at 22:00













        • @KamilKiełczewski Excellent! Thank you so much. I'm going to research into the filter() function now, thanks again Nina and Kamil. But i'm assuming removing the .filter line is not the same as what Nina was saying?

          – Timmy Balk
          Nov 23 '18 at 22:02





















        • Awesome! This works! Is there any way to remove the duplicated arrays from the original array? So the final output of var array would be [[1, 17], [2, 12], [5, 9], [6, 2]]. I tried filter() and indexOf() but it did not work

          – Timmy Balk
          Nov 23 '18 at 21:46











        • for that task, you may use a Set instead of a Map.

          – Nina Scholz
          Nov 23 '18 at 21:55











        • Could you please edit your answer and show me? Do I use has()?

          – Timmy Balk
          Nov 23 '18 at 21:59











        • @TimmyBalk just remove .filter(([, count]) => count > 2) line - and don't change anything else

          – Kamil Kiełczewski
          Nov 23 '18 at 22:00













        • @KamilKiełczewski Excellent! Thank you so much. I'm going to research into the filter() function now, thanks again Nina and Kamil. But i'm assuming removing the .filter line is not the same as what Nina was saying?

          – Timmy Balk
          Nov 23 '18 at 22:02



















        Awesome! This works! Is there any way to remove the duplicated arrays from the original array? So the final output of var array would be [[1, 17], [2, 12], [5, 9], [6, 2]]. I tried filter() and indexOf() but it did not work

        – Timmy Balk
        Nov 23 '18 at 21:46





        Awesome! This works! Is there any way to remove the duplicated arrays from the original array? So the final output of var array would be [[1, 17], [2, 12], [5, 9], [6, 2]]. I tried filter() and indexOf() but it did not work

        – Timmy Balk
        Nov 23 '18 at 21:46













        for that task, you may use a Set instead of a Map.

        – Nina Scholz
        Nov 23 '18 at 21:55





        for that task, you may use a Set instead of a Map.

        – Nina Scholz
        Nov 23 '18 at 21:55













        Could you please edit your answer and show me? Do I use has()?

        – Timmy Balk
        Nov 23 '18 at 21:59





        Could you please edit your answer and show me? Do I use has()?

        – Timmy Balk
        Nov 23 '18 at 21:59













        @TimmyBalk just remove .filter(([, count]) => count > 2) line - and don't change anything else

        – Kamil Kiełczewski
        Nov 23 '18 at 22:00







        @TimmyBalk just remove .filter(([, count]) => count > 2) line - and don't change anything else

        – Kamil Kiełczewski
        Nov 23 '18 at 22:00















        @KamilKiełczewski Excellent! Thank you so much. I'm going to research into the filter() function now, thanks again Nina and Kamil. But i'm assuming removing the .filter line is not the same as what Nina was saying?

        – Timmy Balk
        Nov 23 '18 at 22:02







        @KamilKiełczewski Excellent! Thank you so much. I'm going to research into the filter() function now, thanks again Nina and Kamil. But i'm assuming removing the .filter line is not the same as what Nina was saying?

        – Timmy Balk
        Nov 23 '18 at 22:02















        5














        You can use Object.reduce, Object.entries for this like below






        var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];


        let res = Object.entries(
        array.reduce((o, d) => {
        let key = d.join('-')
        o[key] = (o[key] || 0) + 1

        return o
        }, {}))
        .flatMap(([k, v]) => v > 2 ? [k.split('-').map(Number)] : )


        console.log(res)





        OR may be just with Array.filters






        var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

        let temp = {}
        let res = array.filter(d => {
        let key = d.join('-')
        temp[key] = (temp[key] || 0) + 1

        return temp[key] == 3
        })

        console.log(res)








        share|improve this answer





















        • 1





          First snipped don't meet requirements (return elements are strings not nubmers) - but the second snipped is brilliant, +1

          – Kamil Kiełczewski
          Nov 23 '18 at 21:58













        • Thank you @KamilKiełczewski. Updated first snippet to return Numbers.

          – Nitish Narang
          Nov 24 '18 at 4:19
















        5














        You can use Object.reduce, Object.entries for this like below






        var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];


        let res = Object.entries(
        array.reduce((o, d) => {
        let key = d.join('-')
        o[key] = (o[key] || 0) + 1

        return o
        }, {}))
        .flatMap(([k, v]) => v > 2 ? [k.split('-').map(Number)] : )


        console.log(res)





        OR may be just with Array.filters






        var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

        let temp = {}
        let res = array.filter(d => {
        let key = d.join('-')
        temp[key] = (temp[key] || 0) + 1

        return temp[key] == 3
        })

        console.log(res)








        share|improve this answer





















        • 1





          First snipped don't meet requirements (return elements are strings not nubmers) - but the second snipped is brilliant, +1

          – Kamil Kiełczewski
          Nov 23 '18 at 21:58













        • Thank you @KamilKiełczewski. Updated first snippet to return Numbers.

          – Nitish Narang
          Nov 24 '18 at 4:19














        5












        5








        5







        You can use Object.reduce, Object.entries for this like below






        var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];


        let res = Object.entries(
        array.reduce((o, d) => {
        let key = d.join('-')
        o[key] = (o[key] || 0) + 1

        return o
        }, {}))
        .flatMap(([k, v]) => v > 2 ? [k.split('-').map(Number)] : )


        console.log(res)





        OR may be just with Array.filters






        var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

        let temp = {}
        let res = array.filter(d => {
        let key = d.join('-')
        temp[key] = (temp[key] || 0) + 1

        return temp[key] == 3
        })

        console.log(res)








        share|improve this answer















        You can use Object.reduce, Object.entries for this like below






        var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];


        let res = Object.entries(
        array.reduce((o, d) => {
        let key = d.join('-')
        o[key] = (o[key] || 0) + 1

        return o
        }, {}))
        .flatMap(([k, v]) => v > 2 ? [k.split('-').map(Number)] : )


        console.log(res)





        OR may be just with Array.filters






        var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

        let temp = {}
        let res = array.filter(d => {
        let key = d.join('-')
        temp[key] = (temp[key] || 0) + 1

        return temp[key] == 3
        })

        console.log(res)








        var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];


        let res = Object.entries(
        array.reduce((o, d) => {
        let key = d.join('-')
        o[key] = (o[key] || 0) + 1

        return o
        }, {}))
        .flatMap(([k, v]) => v > 2 ? [k.split('-').map(Number)] : )


        console.log(res)





        var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];


        let res = Object.entries(
        array.reduce((o, d) => {
        let key = d.join('-')
        o[key] = (o[key] || 0) + 1

        return o
        }, {}))
        .flatMap(([k, v]) => v > 2 ? [k.split('-').map(Number)] : )


        console.log(res)





        var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

        let temp = {}
        let res = array.filter(d => {
        let key = d.join('-')
        temp[key] = (temp[key] || 0) + 1

        return temp[key] == 3
        })

        console.log(res)





        var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

        let temp = {}
        let res = array.filter(d => {
        let key = d.join('-')
        temp[key] = (temp[key] || 0) + 1

        return temp[key] == 3
        })

        console.log(res)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 4:08

























        answered Nov 23 '18 at 21:11









        Nitish NarangNitish Narang

        2,9501815




        2,9501815








        • 1





          First snipped don't meet requirements (return elements are strings not nubmers) - but the second snipped is brilliant, +1

          – Kamil Kiełczewski
          Nov 23 '18 at 21:58













        • Thank you @KamilKiełczewski. Updated first snippet to return Numbers.

          – Nitish Narang
          Nov 24 '18 at 4:19














        • 1





          First snipped don't meet requirements (return elements are strings not nubmers) - but the second snipped is brilliant, +1

          – Kamil Kiełczewski
          Nov 23 '18 at 21:58













        • Thank you @KamilKiełczewski. Updated first snippet to return Numbers.

          – Nitish Narang
          Nov 24 '18 at 4:19








        1




        1





        First snipped don't meet requirements (return elements are strings not nubmers) - but the second snipped is brilliant, +1

        – Kamil Kiełczewski
        Nov 23 '18 at 21:58







        First snipped don't meet requirements (return elements are strings not nubmers) - but the second snipped is brilliant, +1

        – Kamil Kiełczewski
        Nov 23 '18 at 21:58















        Thank you @KamilKiełczewski. Updated first snippet to return Numbers.

        – Nitish Narang
        Nov 24 '18 at 4:19





        Thank you @KamilKiełczewski. Updated first snippet to return Numbers.

        – Nitish Narang
        Nov 24 '18 at 4:19











        5














        Try this



        array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))


        Time complexity O(n) (one array pass by filter function). Inspired by Nitish answer.



        Explanation



        The (r={}, a=>...) will return last expression after comma (which is a=>...) (e.g. (5,6)==6). In r={} we set once temporary object where we will store unique keys. In filter function a=>... in a we have current array element . In r[a] JS implicity cast a to string (e.g 1,17). Then in !(2-(r[a]=++r[a]|0)) we increase counter of occurrence element a and return true (as filter function value) if element a occurred 3 times. If r[a] is undefined the ++r[a] returns NaN, and further NaN|0=0 (also number|0=number). The r[a]= initialise first counter value, if we omit it the ++ will only set NaN to r[a] which is non-incrementable (so we need to put zero at init). If we remove 2- as result we get input array without duplicates - or alternatively we can also get this by a=>!(r[a]=a in r). If we change 2- to 1- we get array with duplicates only.






        var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

        var r= array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))

        console.log(JSON.stringify(r));








        share|improve this answer






























          5














          Try this



          array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))


          Time complexity O(n) (one array pass by filter function). Inspired by Nitish answer.



          Explanation



          The (r={}, a=>...) will return last expression after comma (which is a=>...) (e.g. (5,6)==6). In r={} we set once temporary object where we will store unique keys. In filter function a=>... in a we have current array element . In r[a] JS implicity cast a to string (e.g 1,17). Then in !(2-(r[a]=++r[a]|0)) we increase counter of occurrence element a and return true (as filter function value) if element a occurred 3 times. If r[a] is undefined the ++r[a] returns NaN, and further NaN|0=0 (also number|0=number). The r[a]= initialise first counter value, if we omit it the ++ will only set NaN to r[a] which is non-incrementable (so we need to put zero at init). If we remove 2- as result we get input array without duplicates - or alternatively we can also get this by a=>!(r[a]=a in r). If we change 2- to 1- we get array with duplicates only.






          var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

          var r= array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))

          console.log(JSON.stringify(r));








          share|improve this answer




























            5












            5








            5







            Try this



            array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))


            Time complexity O(n) (one array pass by filter function). Inspired by Nitish answer.



            Explanation



            The (r={}, a=>...) will return last expression after comma (which is a=>...) (e.g. (5,6)==6). In r={} we set once temporary object where we will store unique keys. In filter function a=>... in a we have current array element . In r[a] JS implicity cast a to string (e.g 1,17). Then in !(2-(r[a]=++r[a]|0)) we increase counter of occurrence element a and return true (as filter function value) if element a occurred 3 times. If r[a] is undefined the ++r[a] returns NaN, and further NaN|0=0 (also number|0=number). The r[a]= initialise first counter value, if we omit it the ++ will only set NaN to r[a] which is non-incrementable (so we need to put zero at init). If we remove 2- as result we get input array without duplicates - or alternatively we can also get this by a=>!(r[a]=a in r). If we change 2- to 1- we get array with duplicates only.






            var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

            var r= array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))

            console.log(JSON.stringify(r));








            share|improve this answer















            Try this



            array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))


            Time complexity O(n) (one array pass by filter function). Inspired by Nitish answer.



            Explanation



            The (r={}, a=>...) will return last expression after comma (which is a=>...) (e.g. (5,6)==6). In r={} we set once temporary object where we will store unique keys. In filter function a=>... in a we have current array element . In r[a] JS implicity cast a to string (e.g 1,17). Then in !(2-(r[a]=++r[a]|0)) we increase counter of occurrence element a and return true (as filter function value) if element a occurred 3 times. If r[a] is undefined the ++r[a] returns NaN, and further NaN|0=0 (also number|0=number). The r[a]= initialise first counter value, if we omit it the ++ will only set NaN to r[a] which is non-incrementable (so we need to put zero at init). If we remove 2- as result we get input array without duplicates - or alternatively we can also get this by a=>!(r[a]=a in r). If we change 2- to 1- we get array with duplicates only.






            var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

            var r= array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))

            console.log(JSON.stringify(r));








            var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

            var r= array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))

            console.log(JSON.stringify(r));





            var array = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

            var r= array.filter(( r={}, a=>!(2-(r[a]=++r[a]|0)) ))

            console.log(JSON.stringify(r));






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 1 at 10:11

























            answered Nov 23 '18 at 21:19









            Kamil KiełczewskiKamil Kiełczewski

            11.7k86894




            11.7k86894























                3














                For a different take, you can first sort your list, then loop through once and pull out the elements that meet your requirement. This will probably be faster than stringifying keys from the array even with the sort:






                var arr = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]]
                arr.sort((a, b) => a[0] - b[0] || a[1] - b[1])

                // define equal for array
                const equal = (arr1, arr2) => arr1.every((n, j) => n === arr2[j])

                let GROUP_SIZE = 3
                first = 0, last = 1, res =

                while(last < arr.length){
                if (equal(arr[first], arr[last])) last++
                else {
                if (last - first >= GROUP_SIZE) res.push(arr[first])
                first = last
                }
                }
                if (last - first >= GROUP_SIZE) res.push(arr[first])
                console.log(res)








                share|improve this answer
























                • Interesting approach, but is it really faster?

                  – Timmy Balk
                  Nov 23 '18 at 21:52






                • 1





                  @TimmyBalk this isn't a criticism of the JSON approach -- I think it's good, but if my test is correct, it is slower (at least on my browser). Here's a jsperf test: jsperf.com/2json-v-sort-loop

                  – Mark Meyer
                  Nov 23 '18 at 21:55


















                3














                For a different take, you can first sort your list, then loop through once and pull out the elements that meet your requirement. This will probably be faster than stringifying keys from the array even with the sort:






                var arr = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]]
                arr.sort((a, b) => a[0] - b[0] || a[1] - b[1])

                // define equal for array
                const equal = (arr1, arr2) => arr1.every((n, j) => n === arr2[j])

                let GROUP_SIZE = 3
                first = 0, last = 1, res =

                while(last < arr.length){
                if (equal(arr[first], arr[last])) last++
                else {
                if (last - first >= GROUP_SIZE) res.push(arr[first])
                first = last
                }
                }
                if (last - first >= GROUP_SIZE) res.push(arr[first])
                console.log(res)








                share|improve this answer
























                • Interesting approach, but is it really faster?

                  – Timmy Balk
                  Nov 23 '18 at 21:52






                • 1





                  @TimmyBalk this isn't a criticism of the JSON approach -- I think it's good, but if my test is correct, it is slower (at least on my browser). Here's a jsperf test: jsperf.com/2json-v-sort-loop

                  – Mark Meyer
                  Nov 23 '18 at 21:55
















                3












                3








                3







                For a different take, you can first sort your list, then loop through once and pull out the elements that meet your requirement. This will probably be faster than stringifying keys from the array even with the sort:






                var arr = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]]
                arr.sort((a, b) => a[0] - b[0] || a[1] - b[1])

                // define equal for array
                const equal = (arr1, arr2) => arr1.every((n, j) => n === arr2[j])

                let GROUP_SIZE = 3
                first = 0, last = 1, res =

                while(last < arr.length){
                if (equal(arr[first], arr[last])) last++
                else {
                if (last - first >= GROUP_SIZE) res.push(arr[first])
                first = last
                }
                }
                if (last - first >= GROUP_SIZE) res.push(arr[first])
                console.log(res)








                share|improve this answer













                For a different take, you can first sort your list, then loop through once and pull out the elements that meet your requirement. This will probably be faster than stringifying keys from the array even with the sort:






                var arr = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]]
                arr.sort((a, b) => a[0] - b[0] || a[1] - b[1])

                // define equal for array
                const equal = (arr1, arr2) => arr1.every((n, j) => n === arr2[j])

                let GROUP_SIZE = 3
                first = 0, last = 1, res =

                while(last < arr.length){
                if (equal(arr[first], arr[last])) last++
                else {
                if (last - first >= GROUP_SIZE) res.push(arr[first])
                first = last
                }
                }
                if (last - first >= GROUP_SIZE) res.push(arr[first])
                console.log(res)








                var arr = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]]
                arr.sort((a, b) => a[0] - b[0] || a[1] - b[1])

                // define equal for array
                const equal = (arr1, arr2) => arr1.every((n, j) => n === arr2[j])

                let GROUP_SIZE = 3
                first = 0, last = 1, res =

                while(last < arr.length){
                if (equal(arr[first], arr[last])) last++
                else {
                if (last - first >= GROUP_SIZE) res.push(arr[first])
                first = last
                }
                }
                if (last - first >= GROUP_SIZE) res.push(arr[first])
                console.log(res)





                var arr = [[1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]]
                arr.sort((a, b) => a[0] - b[0] || a[1] - b[1])

                // define equal for array
                const equal = (arr1, arr2) => arr1.every((n, j) => n === arr2[j])

                let GROUP_SIZE = 3
                first = 0, last = 1, res =

                while(last < arr.length){
                if (equal(arr[first], arr[last])) last++
                else {
                if (last - first >= GROUP_SIZE) res.push(arr[first])
                first = last
                }
                }
                if (last - first >= GROUP_SIZE) res.push(arr[first])
                console.log(res)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 21:34









                Mark MeyerMark Meyer

                38.6k33159




                38.6k33159













                • Interesting approach, but is it really faster?

                  – Timmy Balk
                  Nov 23 '18 at 21:52






                • 1





                  @TimmyBalk this isn't a criticism of the JSON approach -- I think it's good, but if my test is correct, it is slower (at least on my browser). Here's a jsperf test: jsperf.com/2json-v-sort-loop

                  – Mark Meyer
                  Nov 23 '18 at 21:55





















                • Interesting approach, but is it really faster?

                  – Timmy Balk
                  Nov 23 '18 at 21:52






                • 1





                  @TimmyBalk this isn't a criticism of the JSON approach -- I think it's good, but if my test is correct, it is slower (at least on my browser). Here's a jsperf test: jsperf.com/2json-v-sort-loop

                  – Mark Meyer
                  Nov 23 '18 at 21:55



















                Interesting approach, but is it really faster?

                – Timmy Balk
                Nov 23 '18 at 21:52





                Interesting approach, but is it really faster?

                – Timmy Balk
                Nov 23 '18 at 21:52




                1




                1





                @TimmyBalk this isn't a criticism of the JSON approach -- I think it's good, but if my test is correct, it is slower (at least on my browser). Here's a jsperf test: jsperf.com/2json-v-sort-loop

                – Mark Meyer
                Nov 23 '18 at 21:55







                @TimmyBalk this isn't a criticism of the JSON approach -- I think it's good, but if my test is correct, it is slower (at least on my browser). Here's a jsperf test: jsperf.com/2json-v-sort-loop

                – Mark Meyer
                Nov 23 '18 at 21:55













                1














                ES6:



                const repeatMap = {}

                array.forEach(arr => {
                const key = JSON.stringify(arr)
                if (repeatMap[key]) {
                repeatMap[key]++
                } else {
                repeatMap[key] = 1
                }
                })

                const repeatedArrays = Object.keys(repeatMap)
                .filter(key => repeatMap[key] >= 3)
                .map(key => JSON.parse(key))





                share|improve this answer




























                  1














                  ES6:



                  const repeatMap = {}

                  array.forEach(arr => {
                  const key = JSON.stringify(arr)
                  if (repeatMap[key]) {
                  repeatMap[key]++
                  } else {
                  repeatMap[key] = 1
                  }
                  })

                  const repeatedArrays = Object.keys(repeatMap)
                  .filter(key => repeatMap[key] >= 3)
                  .map(key => JSON.parse(key))





                  share|improve this answer


























                    1












                    1








                    1







                    ES6:



                    const repeatMap = {}

                    array.forEach(arr => {
                    const key = JSON.stringify(arr)
                    if (repeatMap[key]) {
                    repeatMap[key]++
                    } else {
                    repeatMap[key] = 1
                    }
                    })

                    const repeatedArrays = Object.keys(repeatMap)
                    .filter(key => repeatMap[key] >= 3)
                    .map(key => JSON.parse(key))





                    share|improve this answer













                    ES6:



                    const repeatMap = {}

                    array.forEach(arr => {
                    const key = JSON.stringify(arr)
                    if (repeatMap[key]) {
                    repeatMap[key]++
                    } else {
                    repeatMap[key] = 1
                    }
                    })

                    const repeatedArrays = Object.keys(repeatMap)
                    .filter(key => repeatMap[key] >= 3)
                    .map(key => JSON.parse(key))






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 23 '18 at 21:11









                    Ben StewardBen Steward

                    1,135315




                    1,135315























                        1














                        You could also do this with a single Array.reduce where you would only push to a result property if the length is equal to 3:






                        var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

                        console.log(array.reduce((r,c) => {
                        let key = c.join('-')
                        r[key] = (r[key] || 0) + 1
                        r[key] == 3 ? r.result.push(c) : 0 // if we have a hit push to result
                        return r
                        }, { result: }).result) // print the result property








                        share|improve this answer




























                          1














                          You could also do this with a single Array.reduce where you would only push to a result property if the length is equal to 3:






                          var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

                          console.log(array.reduce((r,c) => {
                          let key = c.join('-')
                          r[key] = (r[key] || 0) + 1
                          r[key] == 3 ? r.result.push(c) : 0 // if we have a hit push to result
                          return r
                          }, { result: }).result) // print the result property








                          share|improve this answer


























                            1












                            1








                            1







                            You could also do this with a single Array.reduce where you would only push to a result property if the length is equal to 3:






                            var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

                            console.log(array.reduce((r,c) => {
                            let key = c.join('-')
                            r[key] = (r[key] || 0) + 1
                            r[key] == 3 ? r.result.push(c) : 0 // if we have a hit push to result
                            return r
                            }, { result: }).result) // print the result property








                            share|improve this answer













                            You could also do this with a single Array.reduce where you would only push to a result property if the length is equal to 3:






                            var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

                            console.log(array.reduce((r,c) => {
                            let key = c.join('-')
                            r[key] = (r[key] || 0) + 1
                            r[key] == 3 ? r.result.push(c) : 0 // if we have a hit push to result
                            return r
                            }, { result: }).result) // print the result property








                            var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

                            console.log(array.reduce((r,c) => {
                            let key = c.join('-')
                            r[key] = (r[key] || 0) + 1
                            r[key] == 3 ? r.result.push(c) : 0 // if we have a hit push to result
                            return r
                            }, { result: }).result) // print the result property





                            var array = [[1, 17], [1, 17], [1, 17], [1, 17], [2, 12], [5, 9], [2, 12], [6, 2], [2, 12]];

                            console.log(array.reduce((r,c) => {
                            let key = c.join('-')
                            r[key] = (r[key] || 0) + 1
                            r[key] == 3 ? r.result.push(c) : 0 // if we have a hit push to result
                            return r
                            }, { result: }).result) // print the result property






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 24 '18 at 7:27









                            AkrionAkrion

                            9,48511224




                            9,48511224






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452875%2ffind-if-two-arrays-are-repeated-in-array-and-then-select-them%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Wiesbaden

                                Marschland

                                Dieringhausen