Created
March 7, 2013 08:17
-
-
Save jkaihsu/5106402 to your computer and use it in GitHub Desktop.
Write a method longest_string which takes as its input an Array of Strings and returns the longest String in the Array. For example: # 'zzzzzzz' is 7 characters long
longest_string(['cat', 'zzzzzzz', 'apples']) # => "zzzzzzz"
If the input Array is empty longest_string should return nil.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def longest_string(array) | |
if array.empty? | |
nil | |
else | |
long_string = array.group_by(&:size).max.last | |
long_string[0] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment