Remove duplicate IPs while keeping order
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.
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:
10.0.0.110.0.0.210.0.0.3Approach
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 seenandseen.append(ip) - Java:
!seen.contains(ip)andseen.add(ip) - C++:
std::find(...) == seen.end()means "not found", andseen.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)