How to detect duplicate values ​​in an array in Ruby?

Let's say I have an array that looks like this:

a = [cat, dog, cat, mouse, rat, dog, cat] 

How do I get through this and do something with duplicates - for example. delete them?

In other words, if I did a.each do |i| , then how I evaluate [0], against [1], a [2], a [3] ... and then when I find the one that I want, let's say [2] in this case has the first duplicate, then I push it onto the stack or delete it or something like that.

I know how to evaluate keys as well as values ​​... but how can I evaluate values ​​against each other in the same array?

Thanks.

+6
source share
10 answers

You can create a hash to store the number of repetitions of any item. Thus, iterating through the array only once.

 h = Hash.new(0) ['a','b','b','c'].each{ |e| h[e] += 1 } 

Result

  {"a"=>1, "b"=>2, "c"=>1} 
+11
source

This works efficiently and quite simply:

 require 'set' visited = Set.new array.each do |element| if visited.include?(element) # duplicated item else # first appearance visited << element end end 
+5
source

Try the following:

 class Array def find_dups uniq.map {|v| (self - [v]).size < (self.size - 1) ? v : nil}.compact end end a = ['cat', 'dog', 'cat', 'mouse', 'rat', 'dog', 'cat'] print a - a.find_dups # Removes duplicates 

find_dups will return items with duplicates

+3
source

A simple solution is to run a double loop:

 a.each_with_index do |a1, idx1| a.each_with_index do |a2, idx2| next if idx1 >= idx2 # Don't compare element to itself # and don't repeat comparisons already made # do something with a pair of elements (a1, a2) end end 

If you just want to eliminate duplicates, use the method: Array#uniq .

+1
source

Using a.uniq! to remove duplicates.

also check out ruby-doc.org where you can find more information about the methods of the ruby ​​class.

+1
source

Try the following:

  array.inject ({}) {| h, e |  h [e] = h [e] .to_i + 1;  h}
+1
source

This will print all duplicates in the array:

 array.inject(Hash.new(0)) { |hash,val| hash[val] += 1; hash }.each_pair { |val,count| puts "#{val} -> #{count}" if count > 1 } 
0
source

If you just want to get rid of duplicates, the easiest way is to take an array and an array & array. Use the and operator.

If you want to know what repetitions are, just compare the array with the array and array.

0
source

If the array is sorted, something like below will return only duplicates.

 array.sort.each_cons(2).select {|p| p[0] == p[1] }.map &:first 

Sorts an array, then matches it with consecutive pairs of elements, selects pairs that are the same, matches with elements.

0
source

The best way to do this is to compare it with a unique version of yourself. If it is the same, then it has no duplicates; if not, then there are duplicates.

 unique_array = original_array.uniq 

get a unique version of your array

 if original_array == unique_array then return true else return false 

compare it to the original array.

Simple!

0
source

Source: https://habr.com/ru/post/910154/


All Articles