avatarjb stevenard

Summary

The web content provides an overview of the Set data structure in programming, detailing its properties, operations, and time complexities.

Abstract

The article discusses Sets as a fundamental data structure that stores unique elements in an unordered manner. It emphasizes that Sets are particularly useful for efficiently finding elements without duplicates and for operations that require the determination of uniqueness. The content covers various operations such as insertion, removal, iteration, and counting of elements within a Set. It also delves into the comparison capabilities of Sets, including union, intersection, subtraction, symmetric difference, and subset checks. The article highlights the power of Sets in performing these operations with clear examples and discusses the time complexity associated with each. Additionally, the piece encourages readers to engage further by joining Medium through a referral link for access to more content from the author.

Opinions

  • The author suggests that Sets are the preferred choice when the order of elements is not a concern, and the presence of duplicates is to be avoided.
  • The article implies that the internal implementation of a Set, akin to a dictionary with Boolean values, offers performance benefits for certain types of operations.
  • The author conveys the versatility of Sets by illustrating their use in various comparison operations, which can be particularly useful in algorithms that require set-like manipulations.
  • The inclusion of the author's referral link indicates a desire to build a readership on Medium and implies that the author believes the content provided is valuable enough to warrant a membership.
  • The article is written with the assumption that readers will appreciate the detailed explanations and code examples provided for each Set operation, suggesting a focus on educational value.

Sets

A Set is a data structure representing an unordered collection that contains any elements at maximum once. Elements can be any type (Int, String, Objects, …) but must be hashable (all primitive types implement this protocol). Sets don’t guarantee the ordering.

You will use a set when you don’t care about duplicates and are only interested in efficiently finding elements.

Internally, a set or HashSet is based on a dictionary; the key is the element and the value a Boolean. Then, you get all features of a dictionary plus the one specific to sets:

Insert: To add an element to the set call insert:

Remove: You can also remove an element that will be returned if present:

Iterate: Like all collections, you can easily iterate it:

Count: You can get the number of elements:

One of the great powers of the set is to compare with other sets.

Union: Returns a new set with the elements of both sets without duplicates.

Intersection: Returns a new set with the elements present in both sets.

Subtraction: Returns a new set with the elements from the first set which are not present in the second set.

Symmetric difference: Returns a new set with the unique elements from the first and second set but not the common ones.

Subset: Returns a Boolean indicating if all elements of the first set are present in the second set.

Equal: return a Boolean that indicates if the first set has the same elements as the second set.

Time complexity: (m quantity of first set and n quantity of the second set):

  • Union: O(m+n) ,
  • Intersection: O(m) // could also be O(min(n, m)),
  • Subtraction: O(m),
  • Symmetric difference: O(m+n),
  • isSubset: O(m).

<< Strings | Book | LinkedLists >>

Data Structures
Algorithms
Coding
Programming
Set
Recommended from ReadMedium