Skip to content

Instantly share code, notes, and snippets.

@beng
Created December 11, 2012 01:11
Show Gist options
  • Save beng/4254889 to your computer and use it in GitHub Desktop.
Save beng/4254889 to your computer and use it in GitHub Desktop.
def largest(items, index):
"""
-create a dictionary where the key=item, value=occurrence of item
-get minimum key in dictionary (smallest element)
-add key to ordered list v times (add element number of times it occurs)
-remove pair from dictionary
-return kth element from ordered list
"""
d = collections.defaultdict(int)
order = []
for item in items:
d[item] += 1
while d:
key = min(d.keys())
value = d[key]
order.extend([key] * value)
d.pop(order[-1])
return order[k-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment