Advertisement
bai_onzi

6. symmetric

Jun 8th, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const symmetricSet = (setA, setB) => { // getting two sets from function
  2.   const symmetricSet = new Set();  // creating a new set
  3.  
  4.   setA.forEach(element => {    // checking each element of the first set if it is in the second set
  5.     if(!setB.has(element)) {
  6.       symmetricSet.add(element);
  7.     };
  8.   });
  9.  
  10.   setB.forEach(element => {  // same
  11.     if(!setA.has(element)){
  12.       symmetricSet.add(element);
  13.     };
  14.   });
  15.  
  16.   return Array.from(symmetricSet);  // returning the set as an array
  17. }
  18.  
  19. const setX = new Set([1, 2, 3, 4, 5]);
  20. const setY = new Set([4, 5, 6, 7, 8]);
  21.  
  22. console.log(symmetricSet(setX, setY)); // [ 1, 2, 3, 6, 7, 8 ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement