- Define the characteristics of a set
- Implement a common methods of a set class
A set is a data structure that can store any number of unique values or elements. That means there are no repeating elements in a set.
Many languages provide sets as part of the core language, but today, we'll be
building our own MySet
class by using a hash/object as the underlying data
structure. We've chosen a hash/object because that data structure only allows
unique keys.
// Convert an Array to a Set
array = [1, 3, 1, 2]
set = new Set from array
=> {1, 3, 2}
We have already declared a class called MySet
in the starter files and
provided the underlying data structure: a hash/object called data
. You'll need
to build out all of the methods. Do not worry about the ordering of items in the
set. They can be in any order.
Be aware that these methods may go by different names in different languages.
This is one way to implement a set, there are others! We are basing ours on
JavaScript's Set
class.
We should be able to initialize a new MySet
instance with no data, or by
providing an iterable, such as an array or string. When a new MySet
instance
is initialized with an array or string, only its unique contents should be
added to the MySet
instance.
Use a value of true
for each key: { dog: true }
.
new MySet()
=> MySet data = {}
new MySet([1, 2, 1, 3])
=> MySet data = { 1: true, 2: true, 3: true }
new MySet('hello')
=> MySet data = { 'h': true, 'e': true, 'l': true, 'o': true }
An error should be thrown if a user tries to initialize a new MySet
instance
with anything other than nothing, a string, or an array.
Returns the number of items in the MySet instance.
Add an item to a MySet
instance. Remember, only unique items should exist
in MySet
. When adding an item, the item is added as is. Return the MySet
instance.
my_set = new MySet()
my_set.add('caat')
=> MySet data = { 'caat' }
next_set = new MySet('doooog')
next_set.add('caarp')
=> MySet data = { 'd', 'o', 'g', 'caarp' }
next_set.add(12)
=> MySet data = { 'd', 'o', 'g', 'caarp', 12 }
Note on JS: Arrays cannot be keys for JS objects. They will be converted to
strings by default like so: [1, 2] => '1,2'
. Don't worry about this. We'll
ensure our tests allow for this. For now, just be aware that actual Sets most
certainly can handle Arrays!
Removes the item from the set. If the removal was successful, return true
. If
the item was not removed (i.e. if the item was not present in the set to begin
with), return false
.
my_set = new MySet('aabb')
my_set.delete('a')
=> true
my_set.delete('z')
=> false
Note on JS: Don't worry about handling Arrays here!
If the item is in the MySet
instance, return true
, otherwise false
.
Note on JS: Don't worry about handling Arrays here!
Returns an array containing all of the values in the MySet
instance.
my_set = new MySet('aabb')
my_set.entries()
=> ['a', 'b']
Note on JS: Don't worry about handling Arrays (which were meant to be keys in
this.data
) here!
Use the language of your choosing. We've included starter files for some languages where you can pseudocode, explain your solution and code.
And remember, don't run our tests until you've passed your own!
cd
into the ruby folderruby <filename>.rb
cd
into the javascript foldernode <filename>.js
cd
into the ruby folderbundle install
rspec
cd
into the javascript foldernpm i
npm test