Set - Exercise 2
Question 1: Create a Set Union Completed
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)
function unionSetValues(first, second) {
// Write your solution here
}
console.log(unionSetValues([1,2], [2,3]));function unionSetValues(first, second) {
return [...new Set([...first, ...second])];
}
console.log(unionSetValues([1,2], [2,3]));Question 2: Create a Set Intersection Completed
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)
function intersectSetValues(first, second) {
// Write your solution here
}
console.log(intersectSetValues([1,2,2,3], [3,2]));function intersectSetValues(first, second) {
const other = new Set(second);
return [...new Set(first)].filter((value) => other.has(value));
}
console.log(intersectSetValues([1,2,2,3], [3,2]));Question 3: Create a Set Difference Completed
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)
function differenceSetValues(first, second) {
// Write your solution here
}
console.log(differenceSetValues([1,2,2,3], [2]));function differenceSetValues(first, second) {
const other = new Set(second);
return [...new Set(first)].filter((value) => !other.has(value));
}
console.log(differenceSetValues([1,2,2,3], [2]));Question 4: Check a Set Subset Completed
Return true if every distinct value in the first array occurs in the second.
Expected output: true (print this with console.log)
function isSetSubset(first, second) {
// Write your solution here
}
console.log(isSetSubset([1,2], [2,1,3]));function isSetSubset(first, second) {
const other = new Set(second);
return [...new Set(first)].every((value) => other.has(value));
}
console.log(isSetSubset([1,2], [2,1,3]));Question 5: Compare Set Contents Completed
Return true if the two arrays contain the same distinct values, regardless of order or duplicates.
Expected output: true (print this with console.log)
function sameSetContents(first, second) {
// Write your solution here
}
console.log(sameSetContents([1,2,2], [2,1]));function sameSetContents(first, second) {
const a = new Set(first);
const b = new Set(second);
return a.size === b.size && [...a].every((value) => b.has(value));
}
console.log(sameSetContents([1,2,2], [2,1]));Question 6: Find a Symmetric Difference Completed
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)
function symmetricSetDifference(first, second) {
// Write your solution here
}
console.log(symmetricSetDifference([1,2], [2,3]));function symmetricSetDifference(first, second) {
const a = new Set(first);
const b = new Set(second);
return [...[...a].filter((value) => !b.has(value)), ...[...b].filter((value) => !a.has(value))];
}
console.log(symmetricSetDifference([1,2], [2,3]));Question 7: Count Shared Set Values Completed
Return how many distinct values occur in both arrays.
Expected output: 2 (print this with console.log)
function countSharedSetValues(first, second) {
// Write your solution here
}
console.log(countSharedSetValues([1,2,2,3], [2,3,3]));function countSharedSetValues(first, second) {
const other = new Set(second);
return [...new Set(first)].filter((value) => other.has(value)).length;
}
console.log(countSharedSetValues([1,2,2,3], [2,3,3]));Question 8: Check Disjoint Sets Completed
Return true if the two arrays have no distinct value in common.
Expected output: true (print this with console.log)
function setsAreDisjoint(first, second) {
// Write your solution here
}
console.log(setsAreDisjoint([1,2], [3,4]));function setsAreDisjoint(first, second) {
const other = new Set(second);
return [...new Set(first)].every((value) => !other.has(value));
}
console.log(setsAreDisjoint([1,2], [3,4]));Question 9: List Unique Values Above a Limit Completed
Return distinct numbers greater than the limit in their first-occurrence order.
Expected output: [5,7] (print this with console.log)
function setUniqueValuesAbove(numbers, limit) {
// Write your solution here
}
console.log(setUniqueValuesAbove([2,5,5,7], 3));function setUniqueValuesAbove(numbers, limit) {
return [...new Set(numbers)].filter((number) => number > limit);
}
console.log(setUniqueValuesAbove([2,5,5,7], 3));Question 10: Find the First Repeated Value Completed
Return the first array value that has appeared earlier, or null if no value repeats.
Expected output: "a" (print this with console.log)
function firstRepeatedSetValue(values) {
// Write your solution here
}
console.log(firstRepeatedSetValue(["a","b","a","b"]));function firstRepeatedSetValue(values) {
const seen = new Set();
for (const value of values) { if (seen.has(value)) return value; seen.add(value); }
return null;
}
console.log(firstRepeatedSetValue(["a","b","a","b"]));