Programming from zero

Remove duplicate IPs while keeping order

mediumLists

Problem statement

A list of IP addresses collected from several log files has duplicates. Print each unique IP once, in the order it first appeared, one per line.

Examples

Example 1

Input: ips = ["10.0.0.5", "10.0.0.7", "10.0.0.5", "10.0.0.9", "10.0.0.7"]

Output: 10.0.0.5 10.0.0.7 10.0.0.9

Hints

Approach

Optimal

Loop over the IPs and keep two things: a list unique for the result in order, and a set called seen for fast membership checks. A set is a collection with no duplicates and no order, and ip in seen is very fast even with millions of items. For each IP, if it is not yet in seen, add it to both. Using set(ips) alone would remove duplicates but lose the original order, which matters when the first occurrence is meaningful, such as the first client that hit an endpoint.

Python
ips = ["10.0.0.5", "10.0.0.7", "10.0.0.5", "10.0.0.9", "10.0.0.7"]
seen = set()
unique = []
for ip in ips:
if ip not in seen:
seen.add(ip)
unique.append(ip)
for ip in unique:
print(ip)

Follow-up questions

  • Also print how many duplicates were removed.
  • Print the unique IPs sorted with sorted(unique).

Frequently asked questions

It removes duplicates, but a set does not keep the original order, so the output order can differ. Use it only when order does not matter.