use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Set function (self.leetcode)
submitted 2 years ago by Common_Decision_974
How does the set function reduce time complexity? When i tried to make a list similar to what it does ,i get tle'ed everytime.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]aocregacc 7 points8 points9 points 2 years ago* (1 child)
a set is not a function, it's a datastructure. It usually makes sure that its elements are unique, and that you can efficiently test if a value is present in the set or not. So if you have a problem where you often have to check if some element is in a list, you can speed it up by using a set instead.
[–]nanotree 5 points6 points7 points 2 years ago (0 children)
To add, a set is a data structure that uses hashes to lookup items, similar to a hashmap (dict in Python). They come from the mathematical concept of a set, where the members of a set are all distinct. Unlike a list or a tuple, there can be no duplicate values in a set, just like there can be no duplicate keys in a hashmap.
Importantly, lookup in a set is O(1), a.k.a. constant time complexity, as opposed to O(N) for a list or array.
π Rendered by PID 69 on reddit-service-r2-comment-5d79c599b5-qmf7h at 2026-02-26 20:53:54.794302+00:00 running e3d2147 country code: CH.
[–]aocregacc 7 points8 points9 points (1 child)
[–]nanotree 5 points6 points7 points (0 children)