Given a list of strings logs where each entry is an IP address extracted from a massive log file, return the IP address that appears most frequently.
If multiple IP addresses tie for the highest frequency, return the lexicographically smallest one. The entries come from a very large file, so keep memory use in mind as the input grows.
Function Signature
def most_frequent_ip(logs: list[str]) -> str:
Rules
-
Two entries are the same IP address only when the strings are exactly equal.
-
Ties are broken by plain string comparison, character by character (Python's
<
on
str
). Under this order
"10.0.0.10"
comes before
"10.0.0.9"
, because the character
1
sorts before
9
.
-
Return the IP address as a string.
Constraints
-
1 <= len(logs) <= 10^5
-
Each entry in
logs
is a non-empty string representing an IPv4 address.
Examples
Input: logs = ["192.168.0.1","10.0.0.2","192.168.0.1","10.0.0.2","192.168.0.1"]
Output: "192.168.0.1"
192.168.0.1 appears 3 times while 10.0.0.2 appears 2 times, so the answer is 192.168.0.1.
Input: logs = ["8.8.8.8","1.1.1.1","8.8.4.4","1.1.1.1","8.8.8.8","1.1.1.1"]
Output: "1.1.1.1"
1.1.1.1 appears 3 times, 8.8.8.8 appears 2 times and 8.8.4.4 appears once, so the most frequent is 1.1.1.1.
Input: logs = ["10.0.0.9","10.0.0.10","10.0.0.9","10.0.0.10"]
Output: "10.0.0.10"
Both addresses appear twice. As strings, "10.0.0.10" is smaller than "10.0.0.9", so it wins the tie even though it was seen second.