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

Комментарии • 16

  • @prabeshregmi3372
    @prabeshregmi3372 Год назад

    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]))

  • @penninahgathu7956
    @penninahgathu7956 4 года назад +1

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

  • @brNoMundo123
    @brNoMundo123 2 года назад +1

    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;
    }

  • @jackdavidevans
    @jackdavidevans 4 года назад

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

  • @juanferrer5885
    @juanferrer5885 Год назад

    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)
    }

  • @brianhuynh
    @brianhuynh 3 года назад

    You explain so clear. Thank you very much

  • @anantamawinstead5894
    @anantamawinstead5894 3 года назад

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

  • @nancy7160
    @nancy7160 20 дней назад

    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].

  • @nicolotommasi4811
    @nicolotommasi4811 3 года назад

    thank you very much for this clear explanation!

  • @ahmad-ali14
    @ahmad-ali14 4 года назад +1

    Many Thanks Justin

  • @SrckyBoi
    @SrckyBoi 2 года назад +1

    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;
    }

  • @Azhary01
    @Azhary01 3 года назад +1

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

    • @dgcp354
      @dgcp354 2 года назад +1

      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.

  • @niteshjha8827
    @niteshjha8827 3 года назад

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

  • @hayfachouchene1285
    @hayfachouchene1285 2 года назад

    Amazing video keep it up !! ♥

  • @aaronargottelopez3488
    @aaronargottelopez3488 3 года назад