Having experience with a different language where this was simple, I am finding it challenging to articulate a sequence of pairs:
- Each pair is made up of two basic elements (such as strings or numbers)
- Each element may appear multiple times within the list, but the combination must be unique:
a, k
a, m
b, m
b, n
The 2 key features I require are:
- The ability to add a pair and ensure that if it already exists, it will not be duplicated (for example, attempting to add
a, k
to the current list would not result in a duplicate entry) - I should also be able to verify whether a specific pair exists in the list. For instance, checking for
b, n
should returntrue
, while checking forb, p
should return false.
What would be the most appropriate implementation in typescript?
Initially, my thought was to use Set<[string, string]>
:
const a = new Set<[string, string]>();
a.add(['a', 'k']);
a.add(['a', 'm']);
a.add(['b', 'm']);
a.add(['b', 'n']);
...
However, the challenge arises when comparing objects, causing a.has(['a', 'b'])
to return false
.
Are there any other alternatives worth considering?