[Ruby] Exclude duplicate elements with the uniq method.

This is a personal memo.

You can use the uniq method to eliminate duplicate elements.

--Non-destructive processing. --Using blocks, you can compare after processing each element --In case of duplication, the first element remains. (Delete the element behind)

For arrays

python


arr = [1, 4, 5, 3, 5, 6, 1, 3, 2, 2, 1, 1, 1]
arr.uniq

=> [1, 4, 5, 3, 6, 2]


##Non-destructive confirmation
p arr
=> [1, 4, 5, 3, 5, 6, 1, 3, 2, 2, 1, 1, 1]

** ▼ Processed value ** You can use blocks to compare processed values. It is the corresponding original value that is deleted. (* Not after processing)

object.uniq{|variable|processing}

For example, for an element containing a character string, use to_s to make all the elements into a character string and then compare them.

python


arr1 = [1, 3, 2, "2", "3", 1]

arr1.uniq{|i| i.to_s}
=> [1, 3, 2]
Original value 1 3 2 "2" "3" 1
After treatment "1" "3" "2" "2" "3" "1"
Remaining elements × × ×

Therefore, the output will be the original value [1, 3, 2] corresponding to the first one without duplication.


## For objects You can also delete elements with duplicate values ​​in an object.

object.uniq{|Variable 1,Variable 2|processing}

--Block is required --Two variables are required. The key is entered in variable 1 and the value is entered in variable 2. --Since the output will be an array, use to_h to return it to an object (symbol).

When making a symbol

python


obj = {:a=>1, :b=>2, :c=>3, :d=>2, :f=>3}
obj2 = obj.uniq{|x,y| y}
=> [[:a, 1], [:b, 2], [:c, 3]]}

obj2.to_h
=> {:a=>1, :b=>2, :c=>3}

### When making a hash If you want to make it a hash, apply `to_s` to the key in the block of the` to_h` method to make it a string.

python


obj = {:a=>1, :b=>2, :c=>3, :d=>2, :f=>3}

obj.uniq{|x,y| y}.to_h{|k ,v| [k.to_s, v]}
=> {"a"=>1, "b"=>2, "c"=>3}

Recommended Posts

[Ruby] Exclude duplicate elements with the uniq method.
[Ruby] Extracting elements with slice method
Finding pi with the Monte Carlo method? (Ruby)
Examine the elements in the array using the [Ruby] includes? Method
Integer check method with ruby
[ruby] Method call with argument
Programming with ruby (on the way)
[Ruby] Obtaining even values ​​using the even? Method
[Ruby] From the basics to the inject method
Combine two lists with duplicate elements removed
[Ruby] Define the hierarchy at the same time as Hash initialization with tap method
Come out with a suffix on the method
Publish the app made with ruby on rails
Manage the version of Ruby itself with rbenv
[Competition Pro] Solve the knapsack problem with Ruby
Come out with a suffix on the method 2
Determine the current page with Ruby on Rails
I checked the number of taxis with Ruby
[Ruby basics] How to use the slice method
Ruby to_s method
[Ruby] end_with? method
[Ruby] Method memorandum
About the method
[Ruby] initialize method
Ruby build method
Ruby accessor method
ruby map method
[Ruby] Basic code list. Keep the basics with examples
[Ruby] Cut out a string using the slice method
Feel the basic type and reference type easily with ruby
[Ruby] Exclude and replace specific patterns with regular expressions
[Ruby] The role of subscripts in learning elements in arrays
[Ruby] Method to easily get the receiver type .class
[At Coder] Solve the ABC183 D problem with Ruby
Feel the basic type and reference type easily with ruby 2
[At Coder] Solve the ABC182 D problem with Ruby