Motivation
From a HackerRank challenge.
Example problem
- Say you have the list
[1,3,4,5,5,6,6,6]
. The expected output should be 5.
The solution
- Use
set
to filter out any duplicate numbers. - Use
sorted
to sort the numbers in ascending order. Can specifyreverse = True
if descending order is desired. - Then access the second last number in the list by indexing.
1 | alist = [1,3,4,5,5,6,6,6] |
And you will get 5!
References
- More on slicing/indexing here.