Skip to content

Instantly share code, notes, and snippets.

@mistrikushal
Created January 11, 2016 11:50
Show Gist options
  • Save mistrikushal/f0af4a61d6214419c598 to your computer and use it in GitHub Desktop.
Save mistrikushal/f0af4a61d6214419c598 to your computer and use it in GitHub Desktop.
a small ruby puzzle solution
# 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