Intermediate Algorithm Scripting - Diff Two Arrays

Tell us what’s happening:
I am trying to find a way to filter out duplicate
values from an array.

Your code so far

function diffArray(arr1, arr2) {
  const newArr = arr1
  .concat(arr2) //joining the two arrays
  .sort(function(a, b) {//sorting the array numerically
    return a - b;
  })
  //trying to filter out the duplicate values
  .filter(function(item, pos) {
    return arr1.indexOf(item) == pos; 
})
    console.log(newArr);
    return newArr;
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Diff Two Arrays

Link to the challenge:

If you are intent on trying the filtering out duplicates method, maybe you could try checking each item to see if indexOf is equal to lastIndexOf. If only one of the item exists, it will return true.

Hi, you need to first define the max and min value of the first and last index of the array. Then, initialize the property you want to return. Then, use a for loop to iterate through the contents of first array if the index of the second array is equal to -1. Then, push the contents of the first array which is accessed with the [i] then use the functions global scope to call the diff between arr1 and (arr1, arr2) / (arr2, arr1), finally return newArr… The function is created after the array has been emptied.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.