Exercise 9.01: Generic Set class
In this exercise, we'll create a Set<T>
class that implements that Set
data structure – a data structure that can hold items, without a specific order, and without duplications, using generics.
Follow these steps to implement this exercise:
Note
The code file for this exercise can be found here: https://packt.link/R336a.
- Start by creating a
Set
class that has a genericT
type. This type will be the type of the items in the set:class Set<T> { }
- Next, let's add a constructor that takes some optional initial values. These will need to be an array with items of type
T
, to match ourSet
items:class Set<T> { private items: T[]; constructor(initialItems: T[] = []) { this.items = initialItems; } }
We use default parameters to initialize
initialItems
with an empty array if we haven't been supplied with one – this makes this parameter...