Skip to content

Latest commit

 

History

History
66 lines (47 loc) · 1.5 KB

maps_and_sets_exercise.md

File metadata and controls

66 lines (47 loc) · 1.5 KB

Maps and Sets Exercise

Quick Question #1

What does the following code return?

new Set([1,1,2,2,3,4]) // {1, 2, 3, 4}

Quick Question #2

What does the following code return?

[...new Set("referee")].join("") // "ref"

Quick Questions #3

What does the Map m look like after running the following code?

let m = new Map();
m.set([1,2,3], true);
m.set([1,2,3], false);
// {1: true, 2: true, 3: true, 1: false, 2: false, 3: false}

hasDuplicate

Write a function called hasDuplicate which accepts an array and returns true or false if that array contains a duplicate

const hasDuplicate = array => new Set(array).size !== arr.length

hasDuplicate([1,3,2,1]) // true
hasDuplicate([1,5,-1,4]) // false

vowelCount

Write a function called vowelCount which accepts a string and returns a map where the keys are numbers and the values are the count of the vowels in the string.

const vowelCount = string => {
    const out = new Map();
    const vowels = new Set(["a", "e", "i", "o", "u"]);
    for (let letter of string.toLowercase()) {
        if (vowels.has(letter)) {
            if (out.has(letter)) {
                out.set(letter, out.get(letter) + 1);
            } else {
                out.set(letter, 1);
            }
        }
    }
}

vowelCount('awesome') // Map { 'a' => 1, 'e' => 2, 'o' => 1 }
vowelCount('Colt') // Map { 'o' => 1 }

Solution

See Our solution.