我试图从数组中删除部分重复的值。
['John', 'Johnson', 'Mike', 'Tim', 'Timberland']
我想删除部分重复的值。在这种情况下,我想保留更长的字符串值。
['Johnson', 'Mike', 'Timberland']
有什么好主意吗?
这就是我会做的:
ary = ['John', 'Johnson', 'Mike', 'Tim', 'Timberland']
ary.select {|e| ary.grep(Regexp.new(e)).size == 1 }
# => ["Johnson", "Mike", "Timberland"]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句