brewersfan1976

sock_merchant.rb

Jun 29th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.40 KB | None | 0 0
  1. # hash table reference
  2.  
  3. def sockMerchant(n, ar)
  4.     hash = Hash.new(0)
  5.     ar.each {|key| hash[key] += 1}
  6.     x = 0
  7.     pairs = 0
  8.    
  9.     hash.each do |key, value|
  10.          if value != 1 then
  11.             pairs = pairs + (value / 2)
  12.          end
  13.     end  
  14.    
  15.     return pairs
  16. end
  17.  
  18. n = gets.strip.to_i
  19. ar = gets.strip
  20. ar = ar.split(' ').map(&:to_i)
  21. result = sockMerchant(n, ar)
  22. puts result;
Advertisement
Add Comment
Please, Sign In to add comment