-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19_repeating_elements.rb
More file actions
33 lines (26 loc) · 1.04 KB
/
19_repeating_elements.rb
File metadata and controls
33 lines (26 loc) · 1.04 KB
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
# Repeating Elements
# Write a method to find the two repeating elements in a given array.
def all_repeating_array_values(arr)
new_array = []
arr.each do |value|
if arr.count(value) > 1
new_array << value
end
end
new_array.empty? ? 'No matching values in array' : new_array.uniq
end
# happy path with integers
p all_repeating_array_values([1, 2, 3, 4, 5, 1])
p all_repeating_array_values([1, 2, 3, 4, 5, 1, 2])
p all_repeating_array_values([1, 2, 3, 4, 5, 1, 2, 3])
p all_repeating_array_values([1, 2, 3, 4, 5, 1, 2, 3, 4])
p all_repeating_array_values([1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
# happy path with strings
p all_repeating_array_values(['1', '2', '3', '4', '5', '1'])
p all_repeating_array_values(['1', '2', '3', '4', '5', '1', '2'])
p all_repeating_array_values(['1', '2', '3', '4', '5', '1', '2', '3'])
# no matching values in array
p all_repeating_array_values([1, 2, 3, 4, 5, 6])
p all_repeating_array_values(['1', '2', '3', '4', '5', '6'])
# varied matching data types
p all_repeating_array_values([1, 2, 3, 1, '1', '2', '3', '1'])