Javascript Freecodecamp Algorithm #27: Find the Symmetric Difference

preview_player
Показать описание
Learn how to solve freecodecamp javascript algorithms in various ways! This series is up-to-date with all ES6 and beyond javascript notations

🗄 Resources:

#javascript #algorithms #freecodecamp
Рекомендации по теме
Комментарии
Автор

Thank you for this video!!

I'm from Brazil, and I like your video a lot!!

This video helped me to approach the solution with more flexibility, like create another function to check two arrays one case at the time.

My solution:
const symOfTwo = (arr1, arr2) => {
arr1 = [...new Set(arr1)]; // Remove duplicated numbers
arr2 = [...new Set(arr2)]; // Remove duplicated numbers

const checkArr1 = arr1.filter(value => !arr2.includes(value));
const checkArr2 = arr2.filter(value => !arr1.includes(value));

const output = [...checkArr1, ...checkArr2];

return output;
}


function sym() {
const arrays = [...arguments]; // Get all arguments

let output = arrays[0]; // Get first argument to check

for (let index = 1; index < arrays.length; index++) {
output = symOfTwo(output, arrays[index]);
}

return output;
}

brNoMundo
Автор

Making my way through your algorithm videos and they're really well-explained, great job!

jackdavidevans
Автор

Thank you so much Justin Kim. This was extremely helpful.

penninahgathu
Автор

Wow, thanks. I am still stuck at this challenge but it gives me new insight.

anantamawinstead
Автор

function symm(arg1, arg2) {
const diff = [
...arg1.filter(f => !arg2.includes(f)),
...arg2.filter(f => !arg1.includes(f))
]

return diff;
}

function sym(...args) {

let arr = args.map(arg => [...new Set(arg)]);
let res = arr[0]
for(let i = 1; i < arr.length; i++) {
res = symm(res, arr[i]);
}
return res.sort()
}

console.log(sym([1, 2, 3, 3], [5, 2, 1, 4]))

prabeshregmi
Автор

You explain so clear. Thank you very much

brianhuynh
Автор

Maybe use Frequency counter object to decrease complexity to 2*N?
function symOfTwo(array1, array2) {
const arr = [...new Set(array1), ...new Set(array2)];
const finalArray = [];
const freqCounter = {};
for (let num of arr) {
freqCounter[num] = freqCounter[num] + 1 || 1;
}
for (const [key, value] of Object.entries(freqCounter)) {
if (value === 1) {
finalArray.push(+key);
}
}
return finalArray;
}

SrckyBoi
Автор

function sym(...args) {
const symDif = new Set()
const argsSet = args.map((arr)=> new Set(arr))
for (const arr of argsSet){
for (const num of arr){
if (symDif.has(num)){
symDif.delete(num)
} else {
symDif.add(num)
}
}
}
return Array.from(symDif)
}

juanferrer
Автор

thank you very much for this clear explanation!

nicolotommasi
Автор

function sym(...args) {

const SETS_LIST = args.map(set => [...new Set(set)])

const getDiffValuesSets = (set1, set2) => {
return set1.filter(value => !set2.includes(value))
}

/**
* findSymmetricDifference:
* get the differences between set1 and set2
*/
const findSymmetricDifference = (set1, set2) => {
return [...getDiffValuesSets(set1, set2), ...getDiffValuesSets(set2, set1)]
}

// result stars with the first set (first element) of SET_LIST
let result = SETS_LIST[0]


for(let index = 1; index < SETS_LIST.length; index++){
result = findSymmetricDifference(result, SETS_LIST[index])
result = [...new Set(result)]
}

return result

}

console.log(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]))

// should return [1, 4, 5].

nancy
Автор

can you do a remake of this video with vanilla js?

Azhary
Автор

When we create two sets in symOfTwo can we not take again a set of set1 and set2?

niteshjha
visit shbcf.ru