How to Get Repeated Elements from Ruby Array

How to find and return a duplicate value in array

a = ["A", "B", "C", "B", "A"]
a.detect{ |e| a.count(e) > 1 }

I know this isn't very elegant answer, but I love it. It's beautiful one liner code. And works perfectly fine unless you need to process huge data set.

Looking for faster solution? Here you go!

def find_one_using_hash_map(array)
map = {}
dup = nil
array.each do |v|
map[v] = (map[v] || 0 ) + 1

if map[v] > 1
dup = v
break
end
end

return dup
end

It's linear, O(n), but now needs to manage multiple lines-of-code, needs test cases, etc.

If you need an even faster solution, maybe try C instead.

And here is the gist comparing different solutions: https://gist.github.com/naveed-ahmad/8f0b926ffccf5fbd206a1cc58ce9743e

Find a Duplicate in an array Ruby

Array#difference comes to the rescue yet again. (I confess that @user123's answer is more straightforward, unless you pretend that Array#difference is already a built-in method. Array#difference is probably the more efficient of the two, as it avoids the repeated invocations of count.) See my answer here for a description of the method and links to its use.
In a nutshell, it differs from Array#- as illustrated in the following example:

a = [1,2,3,4,3,2,4,2]
b = [2,3,4,4,4]

a - b #=> [1]
a.difference b #=> [1, 3, 2, 2]

One day I'd like to see it as a built-in.

For the present problem, if:

arr = [1,2,3,4,3,4]

the duplicate elements are given by:

arr.difference(arr.uniq).uniq
#=> [3, 4]

Getting the indexes of duplicate elements in arrays (Ruby)

Taking a page from procedural languages, we could write:

fruits.each_index.select { |i| fruits[i]=="apples" }
#=> [0, 2, 4]

how to get repeated elements from ruby array?

arr = [1,2,3,1,5,2]
arr.group_by {|e| e}.map { |e| e[0] if e[1][1]}.compact

Pretty ugly... but does the job without an n+1 problem.

Duplicate array elements in Ruby

How about

rangers.zip(rangers).flatten

using Array#zip and Array#flatten?

A solution that might generalize a bit better for your second request might be:

rangers.flat_map { |ranger| [ranger] * 2 }

using Enumerable#flat_map.
Here you can just replace the 2 with any value or variable.

How do I detect duplicate values within an array in Ruby?

You can create a hash to store number of times any element is repeated. Thus iterating over array just once.

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

Should result

 {"a"=>1, "b"=>2, "c"=>1}

Ruby: how to repeat/multiply elements in array?

Try this one

array.flat_map { |item| Array.new(5, item) }
[
[ 0] "A",
[ 1] "A",
[ 2] "A",
[ 3] "A",
[ 4] "A",
[ 5] "B",
[ 6] "B",
[ 7] "B",
[ 8] "B",
[ 9] "B",
[10] "C",
[11] "C",
[12] "C",
[13] "C",
[14] "C"
]

how to get the indexes of duplicating elements in a ruby array

 duplicates = arr.each_with_index.group_by(&:first).inject({}) do |result, (val, group)|
next result if group.length == 1
result.merge val => group.map {|pair| pair[1]}
end

This will return a hash where the keys will be the duplicate elements and the values will be an array containing the index of each occurrence.
For your test input, the result is:

{"A"=>[0, 6], "X"=>[1, 2]}

If all your care about is the indices you can do duplicates.values.flatten to get an array with just the indices.
In this case: [0, 6, 1, 2]



Related Topics



Leave a reply



Submit