Set - Exercise 2

Question 1: Create a Set Union

Return distinct values from both arrays, with first-array values before new second-array values.

Expected output: [1,2,3] (print this with console.log)

javascript

function unionSetValues(first, second) {
  // Write your solution here
}

console.log(unionSetValues([1,2], [2,3]));

Question 2: Create a Set Intersection

Return distinct values from the first array that occur in the second, in first-array order.

Expected output: [2,3] (print this with console.log)

javascript

function intersectSetValues(first, second) {
  // Write your solution here
}

console.log(intersectSetValues([1,2,2,3], [3,2]));

Question 3: Create a Set Difference

Return distinct values from the first array that do not occur in the second, preserving first-array order.

Expected output: [1,3] (print this with console.log)

javascript

function differenceSetValues(first, second) {
  // Write your solution here
}

console.log(differenceSetValues([1,2,2,3], [2]));

Question 4: Check a Set Subset

Return true if every distinct value in the first array occurs in the second.

Expected output: true (print this with console.log)

javascript

function isSetSubset(first, second) {
  // Write your solution here
}

console.log(isSetSubset([1,2], [2,1,3]));

Question 5: Compare Set Contents

Return true if the two arrays contain the same distinct values, regardless of order or duplicates.

Expected output: true (print this with console.log)

javascript

function sameSetContents(first, second) {
  // Write your solution here
}

console.log(sameSetContents([1,2,2], [2,1]));

Question 6: Find a Symmetric Difference

Return distinct values appearing in exactly one array: first-array values first, then second-array values.

Expected output: [1,3] (print this with console.log)

javascript

function symmetricSetDifference(first, second) {
  // Write your solution here
}

console.log(symmetricSetDifference([1,2], [2,3]));

Question 7: Count Shared Set Values

Return how many distinct values occur in both arrays.

Expected output: 2 (print this with console.log)

javascript

function countSharedSetValues(first, second) {
  // Write your solution here
}

console.log(countSharedSetValues([1,2,2,3], [2,3,3]));

Question 8: Check Disjoint Sets

Return true if the two arrays have no distinct value in common.

Expected output: true (print this with console.log)

javascript

function setsAreDisjoint(first, second) {
  // Write your solution here
}

console.log(setsAreDisjoint([1,2], [3,4]));

Question 9: List Unique Values Above a Limit

Return distinct numbers greater than the limit in their first-occurrence order.

Expected output: [5,7] (print this with console.log)

javascript

function setUniqueValuesAbove(numbers, limit) {
  // Write your solution here
}

console.log(setUniqueValuesAbove([2,5,5,7], 3));

Question 10: Find the First Repeated Value

Return the first array value that has appeared earlier, or null if no value repeats.

Expected output: "a" (print this with console.log)

javascript

function firstRepeatedSetValue(values) {
  // Write your solution here
}

console.log(firstRepeatedSetValue(["a","b","a","b"]));