Skip to main content
Programming from zero

Remove duplicate IPs while keeping order

mediumLists

Problem statement

Logs often list the same IP many times. To remove repeats while keeping the original order, build a second list called seen. Go through the IPs in order, and add an IP to seen only if it is not already there. The first time you meet an IP it gets added, and every later repeat is skipped.

Common mistake: checking against the original list instead of the one you are building.

TEXT
Wrong: if ip not in ips:

Every IP you are looping over is, by definition, already in ips, so this check is never true and nothing gets added. The check has to look at seen, the list you are filling up.

Your task: given ips = ["10.0.0.1", "10.0.0.2", "10.0.0.1", "10.0.0.3", "10.0.0.2"], print each unique IP once, in the order it first appeared, one per line.

Expected output:

TEXT
10.0.0.1
10.0.0.2
10.0.0.3

Approach

Start with an empty seen list. For each IP, if it is not in seen, add it. Checking a list this way looks at items one by one, so it gets slower as the list grows. That is fine here. Later, in the Sets and tuples section, you will learn a faster way to do the same job.

  • Python: ip not in seen and seen.append(ip)
  • Java: !seen.contains(ip) and seen.add(ip)
  • C++: std::find(...) == seen.end() means "not found", and seen.push_back(ip) adds it

DSA connection: "have I seen this before?" is the heart of hashing, which the DSA patterns sheet covers.

ips = ["10.0.0.1", "10.0.0.2", "10.0.0.1", "10.0.0.3", "10.0.0.2"]
seen = []
for ip in ips:
    if ip not in seen:
        seen.append(ip)
for ip in seen:
    print(ip)