Skip to content

Instantly share code, notes, and snippets.

@codebubb
Created November 25, 2015 10:01
Show Gist options
  • Select an option

  • Save codebubb/6d5b4cc7c7c0ef42ea3b to your computer and use it in GitHub Desktop.

Select an option

Save codebubb/6d5b4cc7c7c0ef42ea3b to your computer and use it in GitHub Desktop.
// Bonfire: Diff Two Arrays
// Author: @codebubb
// Challenge: http://www.freecodecamp.com/challenges/bonfire-diff-two-arrays?solution=function%20diff(arr1%2C%20arr2)%20%7B%0A%20%20var%20newArr%20%3D%20arr1.concat(arr2)%3B%0A%20%20return%20newArr.filter(function(elem%2C%20index)%7B%0A%20%20%20%20%20return%20newArr.lastIndexOf(elem)%20%3D%3D%3D%20index%20%26%26%20newArr.indexOf(elem)%20%3D%3D%3D%20index%3B%0A%20%20%7D)%3B%0A%7D
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function diff(arr1, arr2) {
var newArr = arr1.concat(arr2); // Maybe this could be incorporated inside the filter function? I don't know how!
return newArr.filter(function(elem, index){
return newArr.lastIndexOf(elem) === index && newArr.indexOf(elem) === index;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment