Answers for "Diff Two Arrays"

5

bash test boolean

the_world_is_flat=true
# ...do something interesting...
if [ "$the_world_is_flat" = true ] ; then
    echo 'Be careful not to fall off!'
fi
Posted by: Guest on March-06-2020
1

array differenc javascript

let difference = arr1.filter(x => !arr2.includes(x));
Posted by: Guest on November-10-2020
2

diff two arrays javascript

function diffArray(arr1, arr2) {
  return arr1
    .concat(arr2)
    .filter(item => !arr1.includes(item) || !arr2.includes(item));
}
Posted by: Guest on August-13-2020
2

boolean constants in bash

true, false
Posted by: Guest on April-10-2020
-1

Diff Two Arrays

const diffArray = (arr1, arr2) => {
  // Store the different elements
  const diffArray = []
  // Concat both arrays
  const uniqueArr = [...new Set([...arr1, ...arr2])];
  // OR const uniqueArr = [...new Set(arr1.concat(...arr2))]

  // Loop through the unique array and confirm that each element in it
  // is in both arrays(arr1,arr2), else push element not found in both 
  // arrays to the diffArray and return it
  uniqueArr.forEach(elem => {
    if(!arr1.includes(elem) || !arr2.includes(elem))
      diffArray.push(elem)
  })
  return diffArray
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
// With love @kouqhar
Posted by: Guest on February-26-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language