1

I want to find the intersect of each array elements in a array and take the intersection.

The inputs are array of arrays e.g., "'list_arrays' as mentioned in this script below" The 'filter' is a limit needed to be applied on the total length of intersections observed The out put is expected as array like this "[[2,4]]"

list_arrays = [[1, 2, 3, 4], [2, 5, 6], [1, 5, 8], [8, 2, 4]]
filter = 2

first_element_array = Array.new
list_arrays.each_with_index do |each_array1, index1|        
 list_arrays.each_with_index do |each_array2, index2|
  unless index1 < index2
   intersection = each_array1 & each_array2
   if intersection.length == filter.to_i
    first_element_array.push(intersection)
   end
  end
 end
end
puts first_element_array

This above procedure takes long execution time as I have too long array of array (In million lines). I need a simple rubistic way to handle this problem. Anyone have any simple idea for it?

5
  • 3
    It is not good to present a code (especially when it is not a good one) as a substitute for expressing what you want. Write directly what you want. What is filter = 2? Commented Feb 28, 2013 at 14:56
  • 2
    also, give examples of input/output. The question is very unclear. Commented Feb 28, 2013 at 14:56
  • 1
    There is some more information added in this question. Thanks for notification. Commented Feb 28, 2013 at 15:01
  • @PalaniKannan I see you edited the question, but it did not make much difference. It is still very unclear. Do you seriously think that anyone will understand what you mean by your phrase restriction on intersection elements? Commented Feb 28, 2013 at 15:03
  • Ok. I edited with some more information. I have less experience about making perfect question content here. Your information are valuable and I tried to improve it. But, down voting is discouraging for beginners like me... Anyway thanks for your information :). Commented Feb 28, 2013 at 15:13

1 Answer 1

6

Deciphering your code it seems what you are asking for is "Return the intersections between pair combinations of a collection if that intersection has a certain size (2 in the example)". I'd write (functional approach):

list_arrays = [[1, 2, 3, 4], [2, 5, 6], [1, 5, 8], [8, 2, 4]]
list_arrays.combination(2).map do |xs, ys|
  zs = xs & ys 
  zs.size == 2 ? zs : nil
end.compact
#=> [[2, 4]]

Proposed optimizations: 1) Use sets, 2) Use a custom abstraction Enumerable#map_compact (equivalent to map+compact but it would discard nils on the fly, write it yourself). 3) Filter out subarrays which won't satisfy the predicate:

require 'set'
xss = list_arrays.select { |xs| xs.size >= 2 }.map(&:to_set)
xss.combination(2).map_compact do |xs, ys|
  zs = xs & ys 
  zs.size == 2 ? zs : nil
end
#=> [#<Set: {2, 4}>]
Sign up to request clarification or add additional context in comments.

5 Comments

@PalaniKannan You wrote you want [[2, 4], [2, 8]], which is not what this answer gives. You either wrote your question inaccurately or you chose the wrong answer.
@sawa: list_arrays was edited in the question, I am using the original value.
@tokland I see. The question is very confusing.
The list_arrays in the question is again modified according to answer. Sorry for confusion @sawa. I need more experience to do better than the person with >20K reputations. I apologize for all my mistake. I think, this is not a place for beginners. May be for geeks. Sorry again for all my mistakes.
@PalaniKannan It has nothing to do with being a beginner or experienced in a programming language. It it about being yourself understood. This is a skill required in just about any field where you need to communicate with people.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.