## F1. Earliest Modal Element

An element of a sequence is *modal* if it occurs at least as many times as any other element of the sequence. The earliest modal element is the modal element that first occurs earliest in the sequence. For example, in the sequence

```
[1, 5, 7, 3, 3, 7, 5]
```

the earliest modal element is 5.

Here's pseudocode for an algorithm for finding the earliest modal element in a nonempty sequence S:

```
d <- empty dictionary
m <- 0
for k in S
if k is a key in d
increment d[k]
else
d[k] <- 1
if d[k] > m
m <- d[k]
for k in S
if d[k] = m
return k
```

Argue that this pseudocode is correct. (If it isn't, fix it first.) Then write some tests, implement the pseudocode as a Python function, and check that the tests pass.

Last modified: Thursday, 10 July 2014, 5:33 PM