I am trying to remove duplicates from my 2D array but no success.
Here is what I am doing any idea where am making mistake?
function Remove_duplicates_from_2d_array(data_array) {
if (data_array.length > 0) {
let unique_index_counter = 0;
// loop on target array
for (var a = 0; a < data_array.length; a++) {
var unique_array = data_array[unique_index_counter];
if (a === unique_index_counter) {
continue;
}
console.log('comparing index: ' + a + ' ( ' + data_array[a] + ' ) -- index: ' + a + ' ( ' + data_array[a] + ' )');
if (data_array[a].sort().join(',') === unique_array.sort().join(',')) {
console.log('match it index ' + a + ' - ' + unique_index_counter);
// same arrays
data_array.splice(a, 1);
a = 0; // reset for loop as splice will rebuilt array
}
// a will be equal to data_array length incase there is no match found
if (a === data_array.length) {
unique_index_counter++;
}
if(unique_index_counter != data_array.length) {
a = 0; // reset for loop because we have not checked all items
}
}
return data_array;
} else {
return [];
}
}
var a1 = [1, 2, 3];
b1 = [4, 4, 5];
c1 = [3, 4, 5];
d1 = [4, 4, 5];
var data_array = [];
data_array.push(a1);
data_array.push(b1);
data_array.push(c1);
data_array.push(d1);
console.log('original array.');
console.log(data_array);
var r = Remove_duplicates_from_2d_array(data_array);
console.log('unique array.');
console.log(r); // [[1,2,3],[4,4,5],[3,4,5]]