Dicts, Sets and Collections
Dictionaries and sets: counting occurrences, uniqueness, Counter.
Two tools that lift your solutions to the next level:
• The dict dictionary stores "key → value" pairs and finds a value by key instantly (in O(1)). Typical use — counting: how many times each number or word occurs.
• The set stores only unique values, and the x in s check is instant too. Typical use — "how many distinct?" and fast membership tests.
Python
Counting occurrences with a dictionary. Enter: 1 2 2 3 3 3
A set is built from a list with a single call: set(nums) — duplicates disappear on their own. And for counting occurrences the standard library has a ready-made Counter — it does what the dictionary above does, in one line.
Python
set and Counter. Enter: 1 2 2 3 3 3
Task: read a line of words and print the words that occur exactly once (hint: Counter + a loop over the words preserving order).
Mark the lesson as completed — the final lesson of the course remains.