-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpairing_nums.rb
46 lines (41 loc) · 877 Bytes
/
pairing_nums.rb
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
34
35
36
37
38
39
40
41
42
43
44
45
46
ar = [1,7,2,5,7,2,4,0,3,2,2]
# ar = [10,20,30]
ar = ar.sort!
new_arr = []
ar.map do |num|
index = (ar.index(num)) + 1
if num == ar[index]
new_arr.push(num)
end
end
new_arr = new_arr.group_by(&:itself)
result = new_arr.transform_values{ |v| v.size / 2 }.values.reduce(:+)
if result == nil
return 0
else
return result
end
pp new_arr
# returns the values of pairs that can be found in the given array
# Submitted Solution below:
def sockMerchant(n, ar)
if (n <= 1 || n > 100)
return 0
else
ar = ar.sort!
new_arr = []
ar.map do |num|
index = (ar.index(num)) + 1
if num == ar[index]
new_arr.push(num)
end
end
new_arr = new_arr.group_by(&:itself)
result = new_arr.transform_values{ |v| v.size / 2 }.values.reduce(:+)
if result == nil
return 0
else
return result
end
end
end