Created
January 11, 2016 11:50
-
-
Save mistrikushal/f0af4a61d6214419c598 to your computer and use it in GitHub Desktop.
a small ruby puzzle solution
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
# check first non-repeating element from array | |
arr = [1,200,1,200,3,4,5] | |
res = Array.new | |
arr.detect{|e| res << e unless(arr.count(e) > 1) } | |
puts "First non-repeated element in Array"+arr.to_s+" is : "+ res.first.to_s | |
# check even times repeating elements in array | |
a = [5, 3, 5, 1, 5, 1, 3] | |
res_arr = Array.new | |
a.select{|e| res_arr << e if a.count(e).even? } | |
res_arr.uniq! | |
p "even times repeating elements in Array"+a.to_s+" are: "+res_arr.to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment