forked from adrianeyre/codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalanceArray.rb
More file actions
33 lines (25 loc) · 783 Bytes
/
BalanceArray.rb
File metadata and controls
33 lines (25 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
=begin
Check that the two provided arrays both contain the same number of
different unique items, regardless of order. For example in the following:
[a,a,a,a,b,b] and [c,c,c,d,c,d]
Both arrays have four of one item and two of another, so balance
should return true.
=end
# My Solution
def balance(arr1, arr2)
sort1 = []
sort2 = []
hash1 = Hash.new {|data,key| data[key]=0}
hash2 = Hash.new {|data,key| data[key]=0}
arr1.each {|data| hash1[data] += 1}
arr2.each {|data| hash2[data] += 1}
hash1.each {|data,key| sort1 << key}
sort1.sort!
hash2.each {|data,key| sort2 << key}
sort2.sort!
return sort1 == sort2 ? true : false
end
# Better Solution
def balance(arr1, arr2)
arr1.map { | i | arr1.count(i) }.sort == arr2.map { | i | arr2.count(i) }.sort
end