The Crystal Programming Language Forum

Difference between Set and Array

i come from a ruby background and the concept of Set is something im not familiar with.
How is it different from a regular Array, in what scenarios should i be choosing one over the other?

1 Like

They are similar but a Set is unordered and does not contain duplicates. An array can contain duplicates and order depends on the order items were inserted.

However, the current implementation of Set causes it to be ordered. This is just an implementation detail and should not be relied upon if you need a specific order.

3 Likes

There could be a page in the book explaining the difference between:

  • Array
  • Set
  • Tuple
  • StaticArray
  • Slice
  • Deque

Edit: Deque added

3 Likes

it’s the same as in Ruby, Ruby also has Set

easy confuse with NamedTuple and Hash
{a:1} and {:a => 1}

in ruby, its same.

1 Like

@pynixwang Couldn’t tell ya how many times I’ve made that mistake :joy: