Skip to main content

Set

A container that stores unique elements in no particular order.

Set is an associative container that contains a collection of unique elements. No two elements in a set can have the same value. Sets provide fast insertion, removal, and lookup operations, typically with average constant time complexity. Unlike maps, sets store only the values themselves, not key-value pairs. They are ideal for membership testing, removing duplicates from collections, and mathematical set operations. At the moment, the only types you can store in a Set are: bool, integer, real and string.

Example


var unique_numbers = Set()
unique_numbers.insert(1)
unique_numbers.insert(2)
unique_numbers.insert(1) // Duplicate, won't be added again

print("Set size: ${unique_numbers.size()}") // Will be 2
print("Contains 1: ${unique_numbers.count(1)}") // Will be 1 (true)
print("Contains 3: ${unique_numbers.count(3)}") // Will be 0 (false)

// Remove duplicates from a list
var numbers = [1, 2, 2, 3, 3, 3, 4]
var unique_set = Set(numbers)

Constructors

Set() -> Set

Creates a default-constructed instance of Set.

Set(const Set other) -> Set

Creates a copy of an existing Set object.

Parameters

  • other: The Set object to copy.

Symbols

NameDescription
=Assigns the value of the right-hand side to the left-hand side.

Members

NameDescription
capacityReturns the number of elements that the container has currently allocated space for.
clearClears all elements from the container.
containsChecks if the set contains the specified value.
countReturns the number of occurrences of the specified value in the container.
emptyChecks if the container is empty.
eraseRemoves the first occurrence of the specified value from the container.
extendMerge an unordered set into an unordered set.
insertInserts a value into the unordered set.
maxReturns the maximum element in the container. Throws an exception if the container is empty.
minReturns the minimum element in the container. Throws an exception if the container is empty.
reserveReserves storage for at least the specified number of elements.
sizeReturns the number of elements in the container.