Search code examples
arraysrubyduplicatespartial

Return similar elements of array in Ruby


Say I have such an array:

arr = ['footballs_jumba_10', 'footballs_jumba_11', 'footballs_jumba_12',
       'footballs_jumba_14', 'alpha_romeo_11', 'alpha_romeo_12',
       'alpha_juliet_10', 'alpha_juliet_11']

If I wanted to return duplicates, (assuming any of these strings in the array were exactly identical, I would just

return arr.detect{ |a| arr.count(a) > 1 }

but, what if I wanted to get only duplicates of the first 10 characters of each element of the array, without knowing the variations beforehand? Like this:

['footballs_', 'alpha_rome', 'alpha_juli']

Solution

  • This is quite straightforward with the method Arry#difference that I proposed in my answer here:

    arr << "Let's add a string that appears just once"
      #=> ["footballs_jumba_10", "footballs_jumba_11", "footballs_jumba_12",
      #    "footballs_jumba_14", "alpha_romeo_11", "alpha_romeo_12",
      #    "alpha_juliet_10", "alpha_juliet_11", "Let's add a string that appears just once"]
    
    a = arr.map { |s| s[0,10] }
      #=> ["footballs_", "footballs_", "footballs_", "footballs_", "alpha_rome",
      #    "alpha_rome", "alpha_juli", "alpha_juli", "Let's add "] 
    b = a.difference(a.uniq)
      #=> ["footballs_", "footballs_", "footballs_", "alpha_rome", "alpha_juli"] 
    b.uniq
      #=> ["footballs_", "alpha_rome", "alpha_juli"]