Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This way uses .sort and will return the 3rd greatest number in an array
- def thirdgreatest(nums)
- i = 0
- nums.sort
- nums[2]
- end
- p thirdgreatest([4,5,6,7])
- # This way just uses while/if etc...
- def third_greatest(nums)
- first = nil
- second = nil
- third = nil
- idx = 0
- while idx < nums.length
- value = nums[idx]
- if first == nil || value > first
- third = second
- second = first
- first = value
- elsif second == nil || value > second
- third = second
- second = value
- elsif third == nil || value > third
- third = value
- end
- idx += 1
- end
- return third
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement