Is it possible to check inclusion array inside array?
i want to check if
primary = [1,2,3] includes secondary = [2,3]
i have tried primary.include?(secondary) => false
need to return bool value
If there aren't any duplicate, you can calculate the Array difference, and check if it is empty :
(secondary-primary).empty?
#=> true
subset_of? checks that for every unique element in secondary, there are at least as many elements in primary :
class Array
def count_by
each_with_object(Hash.new(0)) { |e, h| h[e] += 1 }
end
def subset_of?(superset)
superset_counts = superset.count_by
count_by.all? { |k, count| superset_counts[k] >= count }
end
end
Example :
secondary.subset_of?(primary)
#=> true
[2,2].subset_of?([1, 2, 3])
#=> false
This should work for any Array, and it should be faster than other answers for big arrays.
([1,2,3] - [3,4,5,2,1]).empty?
#=> true
([1,2,3,'a'] - [3,4,5,2,1]).empty?
#=> false
[2, 2] ∉ [1, 2], while your solution gives true.test = primary.dup
secondary.all? { |e| test.index(e).tap { |i| test.delete_at(i) if i } }
primary, secondary = [1, 2, 3], [2, 2]
#⇒ false
primary, secondary = [1, 2, 2, 3], [2, 2, 1]
#⇒ true
What’s being done here:
secondary, claiming that all blocks should return truefalse breaking the loop if there is no such element in primaryThe only trick here is using Object#tap to always return true when element found. The element in the primary might be falsey, Array#delete returns the element deleted and we might accidentally return falsey, mistakenly breaking a loop in such a case. We should return true to all? loop as soon as we have the element found, hence tap.
The input to verify that this is the only correct answer here so far:
primary, secondary = [1, 2, 2, 3, nil, nil], [2, 2, 1, nil, nil]
#⇒ true
primary, secondary = [1, 2, 2, 3, nil], [2, 2, 1, nil, nil]
#⇒ false
tap will do here? And before this you assigned the index to i now you have created a block which will do similar thing. I am trying to understand the answer, but right now it is not clear to me.primary = (1..100_000).to_a and secondary = primary.reverse for example.primary = [1,2,3]
secondary = [2,3]
primary.each_cons(secondary.size).include?(secondary)
Enumerable#each_cons takes chunks of the array and iterates by one for each group.
Enumerable is awesome!
Read the Enumerable docs, I learn something new every time.
secondary = [1, 3]Here solution
primary=[1,2,3]
secondary=[2,3]
secondary.all? { |e| primary.include?(e) }
secondary = [2, 2].To check if an array is present inside another array, just use the include? method,
primary = [1,2,3,[1,2]]
secondary = [2,3]
result = primary.include? secondary
# returns false as [2,3] is not present
primary = [1,2,3,[1,2]]
secondary = [1,2]
result = primary.include? secondary
# returns true as [1,2] is present
To check if the elements of secondary array are present inside the primary array, takes care of the duplicate elements also:
result = (secondary.select {|i| secondary.count(i)<=primary.count(i)}.length == secondary.length)
primary, secondary = [1, 2, 3], [2, 2]
# returns false
primary, secondary = [1, 2, 2, 3], [2, 2, 1]
# returns true
a1 = [5, 3, 9, 7, 8, 7, 1, 7]
a2 = [1, 9, 5, 7, 5]
It will check elements of a2 are present in a1 or not,
a2.all?{ |i| a1.include? i } #=> true
true, although a1 only contains one 5.Using
secondary.all? { |e| primary.include?(e) }
Use Intersection
(primary & secondary).size == secondary.uniq.size
If any element of secondary to be present in primary
(primary & secondary).present?
Hope it helps
([1,7] & [2,7]).present? #=> true[1, 2, 2] & [2, 2].
primary = [1, 1, 2]andsecondary = [1, 2, 2]. If so, how would you define "inclusion" in that case?