What is the difference between Set
and Array
in Ruby except for the fact that sets keep unique elements while arrays can keep duplicate elements?
相关问题
- How to get the maximum of more than 2 numbers in V
- Faster loop: foreach vs some (performance of jsper
- Convert Array to custom object list c#
- pick a random item from a javascript array
- Newtonsoft DeserializeXNode expands internal array
相关文章
- C#中 public virtual string Category { get; }这么写会报错:
- Numpy matrix of coordinates
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- PHP: Can an array have an array as a key in a key-
- Segmentation fault with ruby 2.0.0p247 leading to
Another important difference is in the implementation of the
include?
method: an Array compares members based on the result of the==
method, while a Set uses theeql?
method.For me the main difference is that
Set
s are implemented as hashes, so you haveO(1)
membership tests for elements.They are very different.
Array
a[3]
references the 4th object in the array.[1, 'apple', String, 1, :banana]
(this creates and initializes a new Array).Set
Set.new
.Set
is not part of the core, but part of the standard library, and thus needs arequire 'set'
.