This is a great way of learning, growing and sharing. Keep it up please, I'll follow along.
For these kinds of things I really love the expressive power of functional programming that allows me to declaratively process the array (projecting, reducing, filtering, ...).
Your solution works well, no doubt. I just like it a bit more expressive (performance issues put aside), along the lines of (pseudocode):
This is a great way of learning, growing and sharing. Keep it up please, I'll follow along.
For these kinds of things I really love the expressive power of functional programming that allows me to declaratively process the array (projecting, reducing, filtering, ...).
Your solution works well, no doubt. I just like it a bit more expressive (performance issues put aside), along the lines of (pseudocode):
return arr.sortDescending().skip(1).firstOrDefault();
Why not return arr.sortDescending()[1] ?
Depends on the implementation. I was biased to think of LINQ (C#).
If the object returned by 'sortDescending' contains less than two items, the index approach will throw an exception.