Javascript Freecodecamp Algorithm #27: Find the Symmetric Difference
HTML-код
- Опубликовано: 10 сен 2024
- Learn how to solve freecodecamp javascript algorithms in various ways! This series is up-to-date with all ES6 and beyond javascript notations
Start from the beginning of this series! - • Javascript Freecodecam...
Web Developer Roadmap 2020 Edition - • Video
🗄 Resources:
Final Code Solution (n^3): gist.github.co...
Final Code Solution (n^2): gist.github.co...
#javascript #algorithms #freecodecamp
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]))
Thank you so much Justin Kim. This was extremely helpful.
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;
}
Making my way through your algorithm videos and they're really well-explained, great job!
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)
}
You explain so clear. Thank you very much
Wow, thanks. I am still stuck at this challenge but it gives me new insight.
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].
thank you very much for this clear explanation!
Many Thanks Justin
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;
}
can you do a remake of this video with vanilla js?
function sym(...args) {
const arr = [];
for (let i = 0; i < args.length; i++) {
args[i] = [...new Set(args[i])];
args[i].map((x) => {
if (arr.indexOf(x) === -1) {
arr.push(x);
} else {
arr.splice(arr.indexOf(x), 1);
}
});
}
return arr;
}
it took me 2 days but finaly did it.
When we create two sets in symOfTwo can we not take again a set of set1 and set2?
Amazing video keep it up !! ♥