1

I want delete duplicate array from two arrays, but just show one of array, how I can do it ? I want result [1, 4]

const arr1 = [1, 2, 3, 4];
const arr2 = [2, 3, 5, 6]
function arrayUniq(arr1, arr2) {
 enter code here
    }
mazbeib
  • 11
  • 2
  • do you want to return new array `[1,4]` or mutate the arr1 – cmgchess Apr 10 '22 at 10:22
  • If you want to create a new array that only contains the values in `arr1` that are not present in `arr2`, you can do this: `const result = arr1.filter((v) => !arr2.includes(v));` – Titus Apr 10 '22 at 10:22

2 Answers2

1

As @Titus said, just filter the array comparing if one of them have repeated values.

const arr1 = [1, 2, 3, 4];
const arr2 = [2, 3, 5, 6];
function arrayUniq(arr1, arr2) {
 const arrays = [...arr1,...arr2]
 return arrays.filter(a=> !arr2.includes(a))
}
console.log(arrayUniq(arr1,arr2))
ask4you
  • 768
  • 3
  • 14
0
// remove duplicates from arr1 and arr2
function arrayUniq(arr1, arr2) {
    let result = [];

    // Find unique elements in arr1 & push them into result
    arr1.forEach((e) => (arr2.find((f) => f === e) ? null : result.push(e)));

    // Find unique elements in arr2 & push them into result
    arr2.forEach((e) => (arr1.find((f) => f === e) ? null : result.push(e)));

    return result;
}

console.log(arrayUniq(arr1, arr2));
Elar Saks
  • 151
  • 5