Skip to main content

Set

A set is an unordered collection of unique elements, similar to the Set in JavaScript. It is commonly used for removing duplicates.

Task

Split the text into words by spaces, remove duplicate words, and sort them.

JavaScript implementation

const text = "apple banana cherry apple banana";
const words = text.split(" ");
const uniqueWords = [...new Set(words)];
uniqueWords.sort();
console.log(uniqueWords);

Python implementation

text = "apple banana cherry apple banana"
words = text.split(" ")
unique_words = set(words)
sorted_words = sorted(unique_words)
print(sorted_words)

Code Highlight

  • Python uses set() to create a set, while JavaScript uses new Set() to create a set.

Difference Quick View

FeatureJavaScriptPython
Create a setlet mySet = new Set()my_set = set()
Add an elementmySet.add(el)my_set.add(el)
Check sizemySet.sizelen(my_set)
Check if emptymySet.size === 0len(my_set) == 0
Delete an elementmySet.delete(el)my_set.remove(el)
Clear the setmySet.clear()my_set.clear()
Check if a member existslet exist = mySet.has(el)exist = el in my_set
Convert set to arraylet myArr = [...mySet]my_list = list(my_set)
Union of sets-my_set.union(another_set)
Intersection of sets-my_set.intersection(another_set)

Resources