Example – coupons

The first example represents the system that checks whether a one-time coupon has already been used. If so, a suitable message should be presented to the user. Otherwise, the system should inform the user that the coupon is valid and it should be marked as used and cannot be used again. Due to the high number of coupons, it is necessary to choose a data structure that allows for quickly checking whether an element exists in some collection. For this reason, the hash set is chosen as a data structure for storing identifiers of the used coupons. Therefore, you just need to check whether an entered identifier exists in the set.

Let's take a look at the code, which should be added to the Main method in the Program class. The ...

Get C# Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.