Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 27 / 68

Handling sets (data without duplicates) with Set

This lesson covers handling data without duplicates using Julia's Set, with the aim of understanding the set as a data structure.

A Set is a box that cannot hold the same value twice. It is handy for removing duplicates from an array and for checking membership quickly.

The sample code builds a set from the duplicate-laden array [1, 2, 2, 3, 3, 3] with Set(nums), and the duplicates disappear automatically. Note how collect() turns the set back into an array before sort() orders it for display.

A common early stumble is that a Set's ordering is not guaranteed. It will not necessarily be in insertion order as an array is, so to fix the display order you combine collect() and sort().

In professional work, a Set's in also outruns an array's when there is a lot of data, which makes it very convenient for deduplication.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)