Convert every subarray to a string using join.
Use Set to deduplicate then reconvert to Array
Then re-map to original sub array format.
const input = [
[0, 0],
[0, 1],
[1, 0],
[0, 0],
[0, 1],
];
function getUniquesSubArr(input){
return Array.from(
new Set(
input.map(el => el.join(','))
)
)
.map(el => el.split(','))
}
let uniques = getUniquesSubArr(input);
console.log(uniques); // [ [ '0', '0' ], [ '0', '1' ], [ '1', '0' ] ]
About counting occurrences:
- still convert subarray to index a temporary data structure used for counting
- add unique elements to the accumulator if not exists
- increment count if exists
- will return a data structure with value of sub array and count number
function countOcc(input){
return Object.values(input.reduce((acc, el) =>{
let elStr=el.join(',');
if(!acc[elStr]){
acc[elStr]={}
acc[elStr].value=el;
acc[elStr].count=0;
}
acc[elStr].count++;
return acc;
}, {}))
}
console.log(countOcc(input)); // [ { value: [ 0, 0 ], count: 2 }, { value: [ 0, 1 ], count: 2 },{ value: [ 1, 0 ], count: 1 }]
if you like just the count ... well not really recommended IMHO bu just
console.log(countOcc(input).map(el=>el.count)); // [ 2, 2, 1 ]