1
Hatchling0 / 300 XP
0day streak 0stars
Your Python practice
Mock tests Readiness
More
Guest mode · saved on this device.

First, unique and distinct

Three questions come up constantly: "how many different?", "which one came first?", and "which appear only once?".

  • Distinct: len(set(items)). A set keeps one copy of each value.
  • First position: a dict remembers it if you only store a key the first time you see it.
  • Appears once: count first, then scan the list again in its original order and return the first item whose count is 1.
first_seen = {}
for i, x in enumerate(items):
    if x not in first_seen:
        first_seen[x] = i

Dicts remember the order keys were added, so looping over counts.items() visits items in first-appearance order. That is how you break ties by "who showed up first" without extra work.

Read the tie-break rule in the statement. "First seen" and "alphabetical" give different answers.

In the table, watch first_seen stop changing once a letter has been recorded.

See it run

text = "banana"
first_seen = {}
for i, ch in enumerate(text):
    if ch not in first_seen:
        first_seen[ch] = i

Record the position of each letter's first appearance.

Practice first, unique and distinct

Exercise 1

A club writes down each visitor's name in names, but people sometimes sign in with different capital letters.

Return how many different visitors there are, as an integer. Two names are the same visitor when they are equal ignoring upper and lower case.

Rules:

  • An empty list gives 0.
  • Names contain no spaces and are never empty.

Constraints: 0 ≤ len(names) ≤ 10^5.

Example: solution(["Ann", "bob", "ANN"]) returns 2 because "Ann" and "ANN" are the same visitor.

Exercise 2

A control panel logs the id of a lamp every time it is toggled. toggles is the log in order. A lamp is single when its id appears in the log exactly once.

Return the id of the single lamp whose toggle happened earliest in the log. If there is no single lamp, return -1.

Rules:

  • Lamp ids are zero or positive integers, so -1 is never a real id.
  • Earliest means the smallest position in the log, not the smallest id.

Constraints: 0 ≤ len(toggles) ≤ 10^5.

Example: solution([8, 6, 5, 8]) returns 6: both 6 and 5 are single, and 6 was logged first.

Exercise 3

text is a sentence made of lowercase letters and spaces. Ignore the spaces.

Return the letter that occurs most often in text, as a one-character string.

Rules:

  • If several letters tie for the highest count, return the one whose first occurrence comes earliest in text.
  • If text has no letters, return "".

Constraints: 0 ≤ len(text) ≤ 10^5.

Example: solution("tab bat") returns "t": t, a and b each appear twice, and "t" appears first.

Exercise 4

A sensor logs one integer reading per minute in readings. Look at every pair of positions i < j where readings[i] == readings[j].

Return the smallest value of j - i over all such pairs, as an integer. If no reading ever repeats, return -1.

Rules:

  • The pair does not have to be adjacent in the list, but equal readings may repeat many times, so check each reading against the previous time it appeared.
  • An empty or one-element list gives -1.

Constraints: 0 ≤ len(readings) ≤ 8000.

Example: solution([7, 1, 2, 3, 4, 7, 7]) returns 1: the last two 7s are one minute apart.

The explanation and tasks are public. Running this lesson requires an account with course access.

Practice

Loading Python…
More
Loading exercise content…
Visualizer Watch your code run line by line

Run your code, then choose Play to follow its execution.

Memory · names → values
Variables appear here as Python creates them.
Output & plots

One-line daily quest