This is your first visit inside The Matrix. Welcome!

The Matrix has you…

This place will take you into the mirage of learning C# programming language, without any previous coding experience being required

Follow the White Rabbit…█

Tuesday, July 17, 2018 07:42

Sorted Set

Sorted Set is the preferred data structure when we want to have sorted items and also to eliminate duplicate elements. Unfortunately, its performance is worse than that of a Hash Set or a Dictionary. The following program declares a new Sorted Set and adds elements to it:

Sometimes happens that you may want to create a Sorted Set from the elements contained in another data structure, such as an array or list. This can be accomplished directly using the Sorted Set’s constructor:

The most useful methods of the Sorted Set class are the following:

RemoveWhere() – this method can be used to remove all elements that meet a certain condition. This method uses Linq, of which we haven’t learned yet:

Clear() – Just as in the case of the other data structures we learned so far, Clear() can be used to remove all the elements in the collection.
UnionWith() – As in the case of the Hash Set, this method will return a new collection of sorted elements by merging two other collections, of course, removing the elements that are duplicates.
SymmetricExceptWith() – This method will join two collections by taking all the elements that are unique to one of the collections. So, all the elements that are found in one of the collections, but not in the other.

ExceptWith() – This is a subtracting method. It will take two collections and will return all the elements that are not present in one of them.
Overlaps() – This useful method tells us if a collection has any elements in common with our Sorted Set. If at least one element is found in both of them, the result is True, otherwise False.
IntersectWith() – Method for merging two collections by taking all the elements that are present in both of them.
Min(), Max() – Since you cannot use an index to access the elements of a Sorted Set, these methods are useful in the sense that they can offer us the first or the last elements of the collection.

The output will be:

Sorted Set

IsSubsetOf(), IsSupersetOf(), IsProperSubsetOf() and IsProperSupersetOf() – These are methods that help us compute subsets and supersets. In common language, a subset is a set that is contained entirely inside another set. A superset is the set that contains entirely another set. Aside of that, “proper subset” or “proper superset” refers to the property of not having the same exact count of elements. A superset must have at least one element more than the contained set, and a subset must have at least one element less than the set containing it:

Producing this result:

SetEquals() – An useful methods whenever we want to compare two Sorted Sets and check whether they contain the same elements.
GetViewBetween() – a method for taking all the elements in a Sorted Set, between two of its elements, which we provide as parameters to this method. The returned elements are all the elements that are found between these two elements (including them).

The above program will display 4, 5, 7. Why? Because, remember, this is a Sorted Set. So, even if we added the elements in a random order, they will be sorted (numerically, in this case, because we supplied integers). And, after sorting them, the elements that are found between 4 and 7 inclusive, are 4, 5 and 7.

Contains() – Determines whether a Sorted Set contains a certain element.

The most important property of a Sorted Set is:

Count – returns the number of elements in the collection.



Tags: ,

Leave a Reply