The Javascript Function to Check Any Two Sets Are the Same


Two sets are considered same if they contain the same elements which may be of different orders, for example [1, 2, 3] and [3, 2, 1] are the same. In Javascript (ES6) the Set data structure is available and we can convert both sets to arrays using the tripple dots operator and iterate each element in one array (set) and check if it also appears in another. The set.has method checks if an element is in the set. Of course, if the two sets are of different sizes, they must not be the same.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const isSameSet = (s1, s2) => {
  if (s1.size != s2.size) return false;
  const arr1 = [...s1];
  const arr2 = [...s2];
  for (let x of arr1) {
    if (!s2.has(x)) {
      return false;
    }
  }
  for (let x of arr2) {
    if (!s1.has(x)) {
      return false;
    }
  }
  return true;
}
const isSameSet = (s1, s2) => {
  if (s1.size != s2.size) return false;
  const arr1 = [...s1];
  const arr2 = [...s2];
  for (let x of arr1) {
    if (!s2.has(x)) {
      return false;
    }
  }
  for (let x of arr2) {
    if (!s1.has(x)) {
      return false;
    }
  }
  return true;
}

Another method is to use the every method to iterate the elements using the inline function:

1
2
3
4
5
6
const isSameSet = (s1, s2) => {
  if (s1.size !== s2.size) {
    return false;
  }
  return [...s1].every(i => s2.has(i))
}
const isSameSet = (s1, s2) => {
  if (s1.size !== s2.size) {
    return false;
  }
  return [...s1].every(i => s2.has(i))
}

We can also create a new set that merges both sets, and two sets are similar only if the result new set is the same size:

1
2
3
4
const isSameSet = (set1, set2) => {
    let s = new Set([...set1, ...set2])
    return s.size == set1.size && s.size == set2.size
}
const isSameSet = (set1, set2) => {
    let s = new Set([...set1, ...set2])
    return s.size == set1.size && s.size == set2.size
}

We can convert the set to arrays, sort them and convert it to JSON string – then compare both JSON encoded strings to see if they equal.

1
2
3
4
const isSameSet = (s1, s2) => {
  const getComp = v => JSON.stringify([...v].sort());
  return getComp(s1) === getComp(s2)
}
const isSameSet = (s1, s2) => {
  const getComp = v => JSON.stringify([...v].sort());
  return getComp(s1) === getComp(s2)
}

We can also define a contains function that checks if a set contains another – then both sets need to contain another.

1
2
3
4
5
6
const isSameSet = (s1, s2) => {
  function isContain(aSet, bSet){
    return ![...aSet].some(item => !bSet.has(item))
  }
  return isContain(s1, s2) && isContain(s2, s1);
}
const isSameSet = (s1, s2) => {
  function isContain(aSet, bSet){
    return ![...aSet].some(item => !bSet.has(item))
  }
  return isContain(s1, s2) && isContain(s2, s1);
}
JS The Javascript Function to Check Any Two Sets Are the Same javascript

NodeJs / Javascript

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
414 words
Last Post: How to Flatten Javascript Arrays?
Next Post: How to Find First and Last Position of Element in Sorted Array?

The Permanent URL is: The Javascript Function to Check Any Two Sets Are the Same

One Response

  1. Yanick Rochon

Leave a Reply