Editorial for Frequent Flyer


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: kahootist

# Retrieve the first line of input,
# corresponding to the total number of flights taken
number_of_flights = int(input())

# In this case, we wish to keep track of how often each destination is visited,
# so we will use a dictionary to store the count of each destination
counter = {}

# For each of the following lines, corresponding to the destinations visited...
for _ in range(number_of_flights):

    # Retrieve the destination
    destination = input()

    # If the destination has not been visited before, 
    # add it to the dictionary, and initialise the default count to 0
    if destination not in counter:
        counter[destination] = 0

    # Increment the count of the destination
    counter[destination] += 1

# Find the destination that has been visited the most times
max_count = max(counter.values())

# Attempt to find the lexicographically smallest destination that has been visited the most times,
# by going through the dictionary,
# checking if the current destination has the maximum count,
# and override the current best destination if it is the lexicographically smallest one
best_destination = None
for destination in counter:
    if counter[destination] == max_count:
        if best_destination is None or destination < best_destination:
            best_destination = destination

# Print out the final most frequently visited destination!
print(best_destination)

Comments

There are no comments at the moment.