Symmetric Difference -- JavaScript -- Free Code Camp

preview_player
Показать описание

**Symmetric Difference** -- Finding the symmetric difference of 2 or more sets of numbers. Uses `reduce()` and 2 helper functions.
Рекомендации по теме
Комментарии
Автор

Hey Stephen! Thanks for the videos, it's been great to see someone else work through the challenges to get another perspective! This was my solution:

function filter(x, y){
  var one= x.filter(function(e){return this.indexOf(e)<0;}, y);
  var two = y.filter(function(e){return this.indexOf(e)<0;}, x);
  return one.concat(two);
}
function sym(args) {
  var array = filter(arguments[0], arguments[1]);
  if(arguments.length > 2){
    for(var i = 2; i < arguments.length; i ++){
      array = filter(array, arguments[i]);
    }
    return array.reduce(function(x, y){
    if (x.indexOf(y) < 0 ) x.push(y);
    return x;
  }, []);
   
  }
  else return array;
}sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]);

dandyandy
Автор

Pretty cool solution. I went a different route and used Sets. Converting the arguments to sets has the advantage of automatically removing duplicate values. Here is my code:

function sym(args) {
var setA = new Set();
var setB = new Set();

function checkIfHas(element) {
if (setA.has(element)) {
setA.delete(element);
} else {
setA.add(element);
}
}

for (var i = 0; i < arguments.length; i++) {
setB = new
}

return Array.from(setA);
}

PhilippeVaillancourt
Автор

+Stephen Mayeux when converting the arguments to an array, why use instead of Array.from(arguments) ?

PhilippeVaillancourt
Автор

I don't understand how taking out duplicates causes the function to work. It would seem that it would change the dynamic of the array and bring up the wrong result. For example:
[1, 2, 3, 4, 4], [4, 5, 6, 7] I would think would come out to [1, 2, 3, 4, 5, 6, 7] as the extra 4 would stay. Taking out duplicates would give change it to [1, 2, 3, 4], [4, 5, 6, 7] I would think would result in [1, 2, 3, 5, 6, 7] the 4 being removed since it is in both only once. What am I missing?

dajonline
Автор

Hey Stephen, you don't have a tutorial for "Record Collection"? is between "Validate US Telephone Numbers" and "Symmetric Difference" on freecodecamp.

alexlopez
Автор

hey stephen, i tried this approach from your solution on the difference of 2 arrays test as i'm trying to solve it and it only works on 2 arrays, what you think is missing on the code below?

var i;
var argArr = [];
var oneArr;
var newArr = [];

if(Array.isArray(args)){
for(i in arguments){
argArr.push(arguments[i]);
}
}

oneArr = argArr.reduce(function (a, b) {
return a.concat(b);
});

oneArr.filter(function(value, index, array){
&& oneArr.slice(0, index).indexOf(value)==-1){
newArr.push(value);
}
});

return newArr.sort();

paolo-
Автор

There is no Wiki in the Symmetric Difference Free Code Camp page now.

dajonline
Автор

I used Array.prototype.includes() method and also added a second Boolean condition " &&
newArr.includes(array1[i]) === false) " in my function that creates the symmetric diff between two arrays in order to avoid adding a third "removeDupes" function.


function sym(args) {


args = Array.from(arguments);

return args.reduce(function(accumulator, currentValue ){
return diffArray(accumulator, currentValue);

});

}

function diffArray(arr1, arr2) {
var newArr =[];

function checkDiff(array1, array2){

for(var i=0; i<array1.length; i++){
if(array2.includes(array1[i]) === false &&
newArr.includes(array1[i]) === false){
newArr.push(array1[i]);
}
}
}

checkDiff(arr1, arr2);
checkDiff(arr2, arr1);
return newArr;


}

LexNihilus
join shbcf.ru