Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 27 / 68

Working with HashSet (sets)

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

HashSet(Of T) represents a set that holds no duplicates. Unlike an array or a list, adding the same value again collapses into one, and membership checks are fast.

The sample code builds a HashSet from the duplicate-laden array {1, 2, 2, 3, 3, 3} with New HashSet(Of Integer)(nums), and uniqueNums is left holding just 1, 2, and 3. Note Contains() checking whether a value is present.

A common early stumble is that a HashSet's ordering is not guaranteed. It will not necessarily be in insertion order as an array or list is, so where order matters a list suits better.

In professional work, HashSet is used wherever "one of each is enough" — a tag list, checking user IDs for duplicates.

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

๐Ÿงช This site can't compile or run VB.NET 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)