Friday, 17 May 2019

How to remove the duplicate pair in an array using ruby?

Scenario 1
 
a = ['R1,R2','R3,R4','R5,R6', 'R2,R1', 'R4,R3']

Result:

a.uniq {|x| x.split(",").sort.join(',')}

Output => ["R1,R2", "R3,R4", "R5,R6"]


Scenario 2

a = ['R1,R2','R3,R4','R5,R6', 'R2,R1', 'R4,R3']

Result:  

require 'set'
a.uniq {|x| Set.new(x.split(','))} 

Output => ["R1,R2", "R3,R4", "R5,R6"] 

No comments:

Post a Comment

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...