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
setto filter out any duplicate numbers. - Use
sortedto sort the numbers in ascending order. Can specifyreverse = Trueif 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.