1

You have to remove all duplicate elements from the internal arrays of the given 2-D array.

2-D Array

[[1,2,3,8,1,2], [54,26,14,54], [81,4,6,84]]

Internal arrays

[1,2,3,8,1,2], [54,26,14,54] and [81,4,6,84]

So the output should be something along the lines of

[[1,2,3,8],[54,26,15],[81,4,6,84]]

Code:

var twoD = [[1,2,3,8,1,2], [54,26,14,54], [81,4,6,84]];
function deleteDuplicate (twoD) {
        let uniqueArr = []; //init empty array
    for(var i=0;i<twoD.length;i++){
        for(let j of twoD[i]) {
        if(uniqueArr.indexOf(j) === -1) {   
            uniqueArr.push(j);
        }
    }
    }
     return uniqueArr;
}

console.log(deleteDuplicate(twoD));

My code returns a single array with [1,2,3,8,54,26,15,81,4,6,84] as values.

I know the issues lie in the pushing but I am unable to think of anyway, how should I do it?

Not A Bot
  • 2,474
  • 2
  • 16
  • 33
  • This function waits a twoD parameter. However you have set a var twoD in second line. Other hand you should use recursion. – Beller Mar 17 '21 at 06:01
  • Here some more ways you can try :- https://stackoverflow.com/questions/20339466/how-to-remove-duplicates-from-a-two-dimensional-array – Piyush Negi Mar 17 '21 at 06:58

4 Answers4

2

You can use Set.

const arr = [[1,2,3,8,1,2], [54,26,14,54], [81,4,6,84]];


function beUnique(arr){
  const res = arr.map((a)=>{
    const unique = new Set(a);
    return Array.from(unique);
  });
  console.log(res);
}

beUnique(arr);
kyun
  • 9,710
  • 9
  • 31
  • 66
2

Issue

  • You are adding values that satisfy the if condition into one array uniqueArr. That is why the result is 1D array.

Here is the problematic part:

if(uniqueArr.indexOf(j) === -1) {   
  uniqueArr.push(j);
}

Solution

  • Use .map() to modify each inner array.
  • Use new Set() to remove duplicate elements.

var twoD = [
  [1, 2, 3, 8, 1, 2],
  [54, 26, 14, 54],
  [81, 4, 6, 84]
];

function deleteDuplicate(arr) {
  return arr.map(e => [...new Set(e)]);
}

console.log(deleteDuplicate(twoD));

Tips

Here is how to remove duplicate elements from an array.

let arr = [1,1,3,4,5,3];
let newArr = [...new Set(arr)]; // -> [1,3,4,5]
Miu
  • 846
  • 8
  • 22
0

you can try this one

function deleteDuplicate (twoD) {
    var twoD = [[1,2,3,8,1,2], [54,26,14,54], [81,4,6,84]];
        let uniqueArr = []; //init empty array
        let uniqueSubArr = [];
        
    for(var i=0;i<twoD.length;i++){
            uniqueSubArr = [];
        
        for(let j of twoD[i]) {
          if(uniqueArr.indexOf(j) === -1) {   
              uniqueSubArr.push(j);
          }
        }
      
    uniqueArr.push(uniqueSubArr);
    }
     return uniqueArr;
}
0

Using Array.protototype.map(), Array.prototype.reduce() and Array.prototype.includes().

function deleteDuplicate(arr) {
  return arr.map((numbers) => {
    return numbers.reduce((acc, num) => {
      if (!acc.includes(num)) {
        acc.push(num);
      }
      return acc;
    }, []);
  });
}

console.log(
  deleteDuplicate([
    [1, 2, 3, 8, 1, 2],
    [54, 26, 14, 54],
    [81, 4, 6, 84],
  ])
);