Sunday, 15 September 2013

Ruby: 2 arrays merge to keep duplicates -



Ruby: 2 arrays merge to keep duplicates -

i have searched array methods , enumerable methods , not found way this. how merge 2 arrays 1 array, discarding unique values , keeping duplicates?

array1 = [1, 2, 3, 4, 5, 6] array2 = [3, 4, 5, 6, 7, 8]

. . .

array_result = [3, 4, 5, 6 ]

if want items that appear in both arrays (this called intersection) - utilize & operator:

[1, 2, 3, 4, 5, 6] & [3, 4, 5, 6, 7, 8] # => [3, 4, 5, 6 ]

ruby arrays

No comments:

Post a Comment