๐Ÿค” Set in JavaScript ๐Ÿค”

๐Ÿค” Set in JavaScript ๐Ÿค”

How do you get rid of duplications?

ยท

2 min read

What is Set?

It is an object that allows you to store unique values of any type.

Example:

const array = ['hello', 2, 2, 3, 4, 'hi', 'Hi']

const set = new Set(array)

console.log(set) // [object Set]

// need to use either Array.from or spread operator
console.log([...set]) // Array ['hello', 2, 3, 4, 'hi', 'Hi']

Have you noticed that there is only one 2 in the array after console.log? This is how you remove duplications with Set().

Keep in mind, lower and upper case letters are NOT the same. 'A' === 'a' will return false in browser console.

Codewars problem:

An isogram is a word that has no repeating letters, consecutive or non-consecutive.

Implement a function that determines whether a string that contains only letters is an isogram.

Assume the empty string is an isogram. Ignore letter case.

Example: (Input => Output)

  • "Dermatoglyphics" => true

  • "aba" => false

  • "moOse" => false (ignore letter case)

First method (without using Set);

  • Set up the newArr to take characters in

  • Convert str to lower cases

  • Convert str to array with spread operator

  • Check the newArr first, if the newArr doesn't have the character, push. Otherwise, don't push

  • Compare strArr and newArr length, if both are equal, true

function isIsogram(str) {
  const newArr = [];
  const strArr = [...str.toLowerCase()];
  strArr.forEach((char) => {
    if (!newArr.includes(char)) {
      newArr.push(char);
    }
  });
  return newArr.length === str.length
}

Second method:

  • Convert str to lower cases

  • Use new Set() and take str.toLowerCase() as the parameter

  • Use size property to return the number of elements in the Set object and length for str

  • Compare size and length, if both are equal, true.

function isIsogram(str) {
  return new Set(str.toLowerCaser()).size === length
}

// if use spread operator instead
function isIsogram(str) {
  return [...new Set(str.toLowerCase())].length === length
}

Conclusion

Make sure to convert them to lower case first and then remove any duplications. Unless the problem stats differently.

As you can see, both methods work. The first method writes more lines of code, while the second method is one-line!

Resources

MDN - Set

MDN - Set() constructor

MDN - Set.prototype.size

Thank you!

Thank you for your time and reading. I hope this helps you. Please feel free to let me know if you have any questions or feedback. ๐Ÿ˜€

Did you find this article valuable?

Support Victoria's Blog by becoming a sponsor. Any amount is appreciated!

ย