Remove duplicate elements from an array
I have two arrays, array1
and array2
as shown below:
array1 = [ obj11, obj21, obj31 ]
array2 = [ obj21, obj22, obj23 ]
Objects in both arrays belong to the same class. I want to check if it contains array1
objects that already exist in array2
and delete them.
Let's say obj11
and obj22
are equal. By "equal" I mean they have the same attribute values. Then I would like to remove obj11
from array1
and then insert obj21
and obj31
into array2
.
I have already defined equality for attributes in a feature class from here :
def ==(other)
return self.a == other.a && self.b == other.b
end
The resulting array will look like this:
array2 = [ obj21, obj22, obj23, obj21, obj31 ]
source to share
A quick way to remove duplicate values ββfrom multiple arrays is with uniq
array1 = ["dog", "cat", "had"]
array2 = ["big", "fight", "had"]
new_array = (array1 + array2).uniq # <- ["dog", "cat", "had", "big", "fight"]
uniq
removes duplicate values ββfrom an array. By combining array1
and array2
together, you can filter out duplicates between the two of them.
source to share
If I want to solve your problem literally, then I will write something like this:
array1 = [ :obj11, :obj21, :obj31 ] array2 = [ :obj21, :obj22, :obj23 ] new_array = (array1 - array2) + array2 p new_array
(array1 - array2)
takes those elements from array1
which are also present in array2
and adds array2
to what will give you the final result
Output
[:obj11, :obj31, :obj21, :obj22, :obj23]
(Note: I have used symbols as array elements for illustration)
source to share
I got an answer. In the next one, I remove from array1 what already exists in array2. Equality works here as I define it in the question. Thus, checking if the attributes (which are defined in the == method) are equal.
array1.delete_if{|elem| array2.any?{|e| e == elem}}
Then add the rest of the array to array2.
array2 << array1
Then I flattened array2.
array2.flatten!
source to share
You can do it like this:
a2.concat(a1.delete_if { |e| a2.include?(e) })
Here's an example:
class MyClass
attr_reader :a, :b, :c
def initialize(a, b, c)
@a, @b, @c = a, b, c
end
def ==(other)
self.a == other.a && self.b == other.b
end
end
a1 = [MyClass.new('cat', 'bat', 'rat'),
MyClass.new('dog', 'hog', 'pig'),
MyClass.new('jay', 'bee', 'fly'),]
#=> [#<MyClass:0x007fca8407b678 @a="cat", @b="bat", @c="rat">,
# #<MyClass:0x007fca8407bee8 @a="dog", @b="hog", @c="pig">,
# #<MyClass:0x007fca84073ef0 @a="jay", @b="bee", @c="fly">]
a2 = [MyClass.new('fly', 'bee', 'bat'),
MyClass.new('cat', 'bat', 'rat'),
MyClass.new('dog', 'hog', 'cat'),]
#=> [#<MyClass:0x007fca840382d8 @a="fly", @b="bee", @c="bat">,
# #<MyClass:0x007fca840381e8 @a="cat", @b="bat", @c="rat">,
# #<MyClass:0x007fca840380d0 @a="dog", @b="hog", @c="cat">]
a2.concat(a1.delete_if { |e| a2.include?(e) })
#=> [#<MyClass:0x007f96d404ea08 @a="fly", @b="bee", @c="bat">,
# #<MyClass:0x007f96d404e8c8 @a="cat", @b="bat", @c="rat">,
# #<MyClass:0x007f96d404e710 @a="dog", @b="hog", @c="cat">,
# #<MyClass:0x007f96d409d9c8 @a="jay", @b="bee", @c="fly">]
a1
#=> [#<MyClass:0x007f96d409d9c8 @a="jay", @b="bee", @c="fly">]
If we change the first element a1
to:
MyClass.new('cat', 'bat', 'rat')
in
MyClass.new('cat', 'rat', 'bat')
we get:
a2.concat(a1.delete_if { |e| a2.include?(e) })
#=> [#<MyClass:0x007f89528568c0 @a="fly", @b="bee", @c="bat">,
# #<MyClass:0x007f8952856708 @a="cat", @b="bat", @c="rat">,
# #<MyClass:0x007f89528562d0 @a="dog", @b="hog", @c="cat">,
# #<MyClass:0x007f89519277f0 @a="cat", @b="rat", @c="bat">,
# #<MyClass:0x007f8951927598 @a="jay", @b="bee", @c="fly">]
source to share
Another way (using an operation intersection
):
array1 = [ 1, 2, 3, 4, 5 ]
array2 = [ 2, 3, 4, 5, 6 ]
final_array = array1 + array2
final_array & final_array
This will also remove duplicates. IRB output:
2.2.1 :012 > array1 = [ 1, 2, 3, 4, 5 ]
=> [1, 2, 3, 4, 5]
2.2.1 :013 > array2 = [ 2, 3, 4, 5, 6 ]
=> [2, 3, 4, 5, 6]
2.2.1 :014 > final_array = array1 + array2
=> [1, 2, 3, 4, 5, 2, 3, 4, 5, 6]
2.2.1 :015 > final_array & final_array
=> [1, 2, 3, 4, 5, 6]
source to share