Set - Exercise 1

Question 1: Create a Set from Values

Create a Set from the supplied array and return its values as an array in insertion order.

Expected output: ["a","b"] (print this with console.log)

javascript

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

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

Question 2: Count Unique Set Values

Return the number of unique values in the input using a Set.

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

javascript

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

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

Question 3: Check Set Membership

Create a Set from the values and return whether it contains the supplied target.

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

javascript

function setContainsTarget(values, target) {
  // Write your solution here
}

console.log(setContainsTarget(["red","blue"], "blue"));

Question 4: Add a Value to a Set

Create a Set from the input, add the supplied value, and return the values in insertion order.

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

javascript

function addSetValue(values, value) {
  // Write your solution here
}

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

Question 5: Remove a Value from a Set

Create a Set from the input, delete the supplied value, and return its remaining values in insertion order.

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

javascript

function removeSetValue(values, value) {
  // Write your solution here
}

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

Question 6: Report a Set Deletion

Create a Set from the input, delete the supplied value, and return whether an element was removed.

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

javascript

function reportSetDeletion(values, value) {
  // Write your solution here
}

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

Question 7: Clear a Set

Create a Set from the values, record its size, clear it, and return an array with its sizes before and after clearing.

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

javascript

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

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

Question 8: Add Several Set Values

Create a Set from the first array, add each value from the second, and return the result in insertion order.

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

javascript

function addMultipleSetValues(initial, added) {
  // Write your solution here
}

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

Question 9: Iterate over a Set

Create a Set from the numeric input and return the sum of its unique values.

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

javascript

function sumUniqueSetValues(numbers) {
  // Write your solution here
}

console.log(sumUniqueSetValues([2,2,3]));

Question 10: Return Set Values in Reverse

Create a Set and return its distinct values in reverse insertion order.

Expected output: ["c","b","a"] (print this with console.log)

javascript

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

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