Container which checks for the uniqueness of the content
-
21-08-2019 - |
Question
Is there any datatype /container in any language which checks for the uniqueness of the content rather than the value or index before inserting the item?
Solution
Yes, it's called a set. Most languages implement them in some form.
OTHER TIPS
Sure, one that comes to mind is Cocoa's NSSet class. The C++ STL also has a set class.
Of course, you need some way to define a unique object beyond simple pointer or reference comparison. For instance, in Cocoa, two objects are considered the same for the purpose of NSSet if they respond to isEqual:
with YES
and have the same hash code.
The problem with that is that the value can be ANYTHING so that's very open ended and not always pratical.
SQL with a UNIQUE field. sets in Python. Filenames on Filesystems. Depends on how you define "content".