Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const symmetricSet = (setA, setB) => { // getting two sets from function
- const symmetricSet = new Set(); // creating a new set
- setA.forEach(element => { // checking each element of the first set if it is in the second set
- if(!setB.has(element)) {
- symmetricSet.add(element);
- };
- });
- setB.forEach(element => { // same
- if(!setA.has(element)){
- symmetricSet.add(element);
- };
- });
- return Array.from(symmetricSet); // returning the set as an array
- }
- const setX = new Set([1, 2, 3, 4, 5]);
- const setY = new Set([4, 5, 6, 7, 8]);
- console.log(symmetricSet(setX, setY)); // [ 1, 2, 3, 6, 7, 8 ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement