LetMyPeopleCode.com

A blog about software, schmaltz, and monkey-patches for the soul

Menu
  • About Greg Bulmash
  • Greg Talks Tech
  • Privacy Policy
Menu
"The Coding Interview"

The Coding Interview: Find the Symmetric Difference

Posted on January 25, 2022January 25, 2022 by Greg Bulmash

This series answers coding interview questions from the Coding Interview Prep collection at freeCodeCamp. You can find more with the interview questions tag.

The concept is to find the numbers in two arrays that do not repeat in both. So [1,2] and [2,3] would produce [1,3]. If you’re comparing more than 2 arrays, you compare the next array (and beyond) to the result of the last comparison. So [1,2], [2, 3], and [2,4] would produce [1,2,3,4] because 2 is filtered out of the first comparison and it isn’t in the result being compared in the next, so it comes back.

Solution explained

First, the args argument (provided in the scaffolded function) only gives you the first array submitted as an argument. It should just be ignored. Use the arguments object instead to get an array of all the arguments.

  • Create an array(result) to hold the results.

  • Run through the arguments array with a loop (i). Start by filtering duplicates from the array using the spread operator and Set function.

  • With another loop (j), check each number in the filtered set to see if it appears in result. If it’s not there, add it with push. If it’s already there, remove it with slice.

  • The result of the first pass of the loop will add the first array to the loop in its entirety. The result of each successive pass will perform a symmetric compare between the next array (arguments[i]) with the result array.

  • Finally, do a sort on result (requiring the function we pass to result.sort() as an argument to ensure a numeric sort instead of alphabetic) because the tests require the result to be sorted to pass.

Solution

function sym(args) {
  let result = [];
  for(let i in arguments){
    let filtered = [... new Set(arguments[i])];
    for(let j in filtered){
      let number = filtered[j];  
      if(result.indexOf(number) === -1){
          result.push(number);
        } else {
          result.splice(result.indexOf(number), 1);
        }
      }
    }
  result.sort(function(a, b) {
    return a - b;
  });
  return result;
}

 

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Job Hunt Diary 2023: Verizon Is Passive-Aggressive?
  • Job Hunt Diary 2023: Eliciting Responses
  • Job Hunt Diary 2023: And so on, and so on…
  • Job Hunt Diary 2023: Freelancing and warning words
  • Job Hunt Diary 2023: Not all job fairs are created equal

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • September 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • November 2021
  • May 2021
  • April 2020
  • March 2020

Categories

  • Apps
  • Computing
  • Frameworks and Libraries
  • Games
  • Hacks
  • Hardware
  • JavaScript
  • Mac
  • Mobile
  • Other Art Projects
  • Productivity
  • Programming
  • Projects
  • Python
  • Society & Culture
  • Teaching Code
  • Tech Career
  • Uncategorized
  • WebTech
  • Writing

Tag Cloud

Adobe advertising bad ads browser hunt clown car code sample comcast commute copy writing CX design Developer Relations dev rel evernote game demo google drive hardware review health home office home studio internet interview questions j job hunt job hunt diary Mac magento meta-coding OneNote PEO productivity review speedtest t-mobile Windows xfinity

©2023 LetMyPeopleCode.com | Theme by SuperbThemes