Added a more clever solution that doesn't involve iterating over the entire array.