Set - Exercise 3
Question 1: Count Unique Words Ignoring Case Completed
Return the number of distinct words after converting each to lowercase.
Expected output: 2 (print this with console.log)
function countCaseInsensitiveSetWords(words) {
// Write your solution here
}
console.log(countCaseInsensitiveSetWords(["Cat","cat","DOG"]));function countCaseInsensitiveSetWords(words) {
return new Set(words.map((word) => word.toLowerCase())).size;
}
console.log(countCaseInsensitiveSetWords(["Cat","cat","DOG"]));Question 2: Keep First Unique Names Ignoring Case Completed
Return the first spelling of each name, treating names as equal after lowercasing.
Expected output: ["Ada","Bo"] (print this with console.log)
function firstCaseInsensitiveNames(names) {
// Write your solution here
}
console.log(firstCaseInsensitiveNames(["Ada","ada","Bo"]));function firstCaseInsensitiveNames(names) {
const seen = new Set();
return names.filter((name) => { const key = name.toLowerCase(); if (seen.has(key)) return false; seen.add(key); return true; });
}
console.log(firstCaseInsensitiveNames(["Ada","ada","Bo"]));Question 3: Remove Repeated IDs Completed
Return the input records whose id occurs for the first time, preserving their order.
Expected output: [{"id":1,"name":"A"},{"id":2,"name":"C"}] (print this with console.log)
function firstRecordForEachId(records) {
// Write your solution here
}
console.log(firstRecordForEachId([{"id":1,"name":"A"},{"id":1,"name":"B"},{"id":2,"name":"C"}]));function firstRecordForEachId(records) {
const seen = new Set();
return records.filter((record) => { if (seen.has(record.id)) return false; seen.add(record.id); return true; });
}
console.log(firstRecordForEachId([{"id":1,"name":"A"},{"id":1,"name":"B"},{"id":2,"name":"C"}]));Question 4: Find Repeated Values Once Completed
Return each value that occurs more than once, ordered by when it is first repeated, without duplicates.
Expected output: [2,1] (print this with console.log)
function repeatedSetValuesOnce(values) {
// Write your solution here
}
console.log(repeatedSetValuesOnce([1,2,2,1,2]));function repeatedSetValuesOnce(values) {
const seen = new Set();
const repeated = new Set();
for (const value of values) { if (seen.has(value)) repeated.add(value); else seen.add(value); }
return [...repeated];
}
console.log(repeatedSetValuesOnce([1,2,2,1,2]));Question 5: Check Whether Values Are Unique Completed
Return true if no value appears more than once in the array.
Expected output: true (print this with console.log)
function allSetValuesUnique(values) {
// Write your solution here
}
console.log(allSetValuesUnique([1,2,3]));function allSetValuesUnique(values) {
return new Set(values).size === values.length;
}
console.log(allSetValuesUnique([1,2,3]));Question 6: Count Unique Array Keys Completed
Return the number of distinct id values in the supplied objects.
Expected output: 2 (print this with console.log)
function countUniqueObjectIds(records) {
// Write your solution here
}
console.log(countUniqueObjectIds([{"id":1},{"id":1},{"id":2}]));function countUniqueObjectIds(records) {
return new Set(records.map((record) => record.id)).size;
}
console.log(countUniqueObjectIds([{"id":1},{"id":1},{"id":2}]));Question 7: Preserve Set Reinsert Order Completed
Create a Set from the values, delete the supplied value, add it again, and return the resulting insertion order.
Expected output: [1,3,2] (print this with console.log)
function reinsertSetValue(values, value) {
// Write your solution here
}
console.log(reinsertSetValue([1,2,3], 2));function reinsertSetValue(values, value) {
const set = new Set(values);
set.delete(value);
set.add(value);
return [...set];
}
console.log(reinsertSetValue([1,2,3], 2));Question 8: Return Common Unique Letters Completed
Return distinct ASCII characters from the first string that occur in the second, in their first-string order.
Expected output: ["a"] (print this with console.log)
function commonUniqueAsciiLetters(first, second) {
// Write your solution here
}
console.log(commonUniqueAsciiLetters("banana", "apple"));function commonUniqueAsciiLetters(first, second) {
const other = new Set(second);
return [...new Set(first)].filter((char) => other.has(char));
}
console.log(commonUniqueAsciiLetters("banana", "apple"));Question 9: Track Set Size after Adds Completed
Start with an empty Set. Add each input value and return an array containing the size after each addition.
Expected output: [1,2,2,3] (print this with console.log)
function setSizesAfterEachAdd(values) {
// Write your solution here
}
console.log(setSizesAfterEachAdd([1,2,1,3]));function setSizesAfterEachAdd(values) {
const set = new Set();
return values.map((value) => { set.add(value); return set.size; });
}
console.log(setSizesAfterEachAdd([1,2,1,3]));Question 10: Return Set Deletion Results Completed
Create a Set from initial values. Delete each requested value in order and return the booleans from delete.
Expected output: [true,false,false,true] (print this with console.log)
function setDeletionResults(initial, requested) {
// Write your solution here
}
console.log(setDeletionResults([1,2], [2,2,3,1]));function setDeletionResults(initial, requested) {
const set = new Set(initial);
return requested.map((value) => set.delete(value));
}
console.log(setDeletionResults([1,2], [2,2,3,1]));