# [Ruby] ruby API problem

#### Question 1

Let’s create a method that outputs the sum of any three numbers. However, if the same number is included Does not count towards the total.

*If you do not understand, use the API to solve the problem. Reference URL: https://docs.ruby-lang.org/ja/search/

##### Output example:

lone_sum([1, 2, 3]) → 6 lone_sum([3, 2, 3]) → 2 lone_sum([3, 3, 3]) → 0

The problem this time was a difficult and totally irrelevant answer! I didn’t quite understand that the argument was an array. However, when the array appears as a tendency, each statement feels like an iron plate.

Below is the answer.

```
def lone_sum(ary)
Extract only unique elements from # array
uniq_nums = []
ary.each do |num|
count = 0
ary.each do |i|
if num == i
count += 1
end
end
if count <2
uniq_nums << num
end
end
# sum in uniq_nums array
sum = 0
uniq_nums.each do |unique_num|
sum += unique_num
end
puts sum
end
```

##### Explanation

For example, suppose you call the method with lone_sum([3, 2, 3]). ① ary = [3,2,3] ② Use ary.each do |num| to retrieve the contents of the array one by one. ③ The first number retrieved is 3, so num=3. ④ In the second each sentence, ary.each do |i|, i = 3,2,3. ⑤ When the conditional expression if num (3 is taken out) == i, that is, when there are duplicates, the count increases. (6) Add a value to the uniq_nums array only when there is one duplicate value in the 11th row. When num=3, count=2, so no value is added to the uniq_nums array. ⑦ As a result of repeating this two more times, only 2 is added to the uniq_nums array. ⑧ The sum in the uniq_nums array is written with sum=0 or less. ⑨ In this case, sum = sum + 2, that is, sum = 0 + 2. Finally, the total is output as 2.

#### Impressions

I understand, but how long will it take to solve this in the future? If anyone knows what kind of study method can be used to get closer to the correct answer, please let me know. For the time being, all I can do is study.