Is It A Pangram? Part 2

As mentioned in my previous blog, I’ll be going over another solution for this question that’s very efficient. Keep in mind that this question will be solved in JavaScript! In case you didn’t read my last blog, here’s the prompt:

In my last blog, I used hash tables to solve this problem. For this blog, I’ll be covering how to solve this problem using Sets. Sets are very similar to hash tables in the sense that they’re both objects. For more info on Sets make sure to check out the resources section below.

To start off, I’ll create a variable that’s going to represent the set that I’m going to create. Here’s what the code looks like so far:

Next, I need to create an iteration that checks each letter inside the string sentence. Since the for of loop will be checking each letter in sentence, the current letter being iterated will be added into mySet. If the specific letter already exists inside of mySet, then that letter would be ignored. I’ll leave a console.log() inside of the for of loop to show what’s happening during the iteration.

This is what the code looks like so far:

This is what mySet looks like:

As we can see mySet is only holding unique characters which is why we see {“l”, “e”,”t”,”c”,”o”,”d”} instead of the original string ”leetcode”.

Finally, we need to return a boolean statement. We know that there are 26 letters in the alphabet and we also know that mySet is only storing letters that are unique. This means that we can use the size or length of mySet to determine whether or not it is a pangram! For this part we can either use an if conditional or a ternary operator.

Here’s the full solution:

Here are the results for the examples on lines 28–30:

Here are the results on LeetCode:

I hope this blog was helpful in any way! For more info on for of loops, ternary operators and sets, checkout the resource section below!

Resources: