CompTIA Tech+ (FC0-U71)Software Development ConceptsHard
A developer is creating a program that needs to store a collection of unique email addresses. The program should efficiently add new addresses and quickly check if an address already exists, without allowing duplicates. Which data structure is best suited for this requirement?
- AStack
- BLinked List
- CSet
- DArray
Show answer & explanationAnswer & explanation
Correct answer: C. Set
A Set is a data structure that stores unique elements and provides efficient operations for adding elements and checking for membership. It automatically handles duplicate removal, making it ideal for collections where uniqueness is a primary requirement.
Why the other options are wrong
- A. A stack is a Last-In, First-Out (LIFO) data structure, not designed for storing unique collections or efficient membership checks.
- B. A linked list is a linear collection of elements, allows duplicates, and checking for uniqueness requires traversing the list, which is inefficient.
- D. An array stores elements in an ordered collection but allows duplicates and requires manual checking for uniqueness, which can be inefficient.
Set Data Structure
A collection data structure that stores unique elements, meaning no two elements in a set can be identical. Sets support efficient operations for adding elements, removing elements, and checking for membership (presence of an element).
- Elements are unordered (in most implementations).
- Automatically handles duplicate removal.
- Provides fast lookup, insertion, and deletion operations (often O(1) on average).
- Useful for membership testing, eliminating duplicates, and mathematical set operations (union, intersection).
Memory trick: Set: A 'set' of unique items, no duplicates allowed.