# Iterate through array twice

**URL:** <https://www.ruby-forum.com/t/iterate-through-array-twice/129310>\
**Category:** Ruby\
**Created:** [February 16, 2008, 3:51pm UTC](https://www.ruby-forum.com/t/iterate-through-array-twice/129310 "2008-02-16T15:51:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruhe](https://www.ruby-forum.com/u/Ruhe)\
**Post date:** [February 16, 2008, 3:51pm UTC](https://www.ruby-forum.com/t/iterate-through-array-twice/129310/1 "2008-02-16T15:51:01Z")

</div>

I have array of horizontal segments and I need to find which of them  
may be sides of a box, so I implemented this method:

def find\_square\_sides(horiz\_segments)  
squares = Array.new  
horiz\_segments.each\_with\_index do |side, i|  
horiz\_segments[(i+1)…(horiz\_segments.size - 1)].each do |  
condidate|  
if(## here goes long-long check ##)  
squares \<\< Square.new(side, condidate)  
end  
end  
end  
squares  
end

Knowing the beauty of Ruby, I hope that there is a better solution. I  
guess that double iterating isn’t the best.

---

<div class="post-metadata">

**Author:** ![Ruhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruhe](https://www.ruby-forum.com/u/Ruhe)\
**Post date:** [February 16, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/iterate-through-array-twice/129310/2 "2008-02-16T16:20:59Z")

</div>

On 16.02.2008 15:46, Ruhe wrote:

> ```
> end
> end
> 
> ```
> 
> end  
> squares  
> end
> 
> Knowing the beauty of Ruby, I hope that there is a better solution. I  
> guess that double iterating isn’t the best.

Unfortunately the problem you are trying to solve is O(n\*n) and there is  
nothing that will make this go away - at least not with the knowledge we  
have so far. It may be that elements to compare to can be picked more  
efficiently but if you have to compare each with each other your  
solution is probably as good as it gets.

Kind regards

robert

PS: The only small improvement that came to mind was to use -1 as ending  
index for the range of the second iteration.

---

<div class="post-metadata">

**Author:** ![Ruhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruhe](https://www.ruby-forum.com/u/Ruhe)\
**Post date:** [February 16, 2008, 7:21pm UTC](https://www.ruby-forum.com/t/iterate-through-array-twice/129310/3 "2008-02-16T19:21:10Z")

</div>

With Ruby 1.9:

def find\_square\_sides(horiz\_segments)  
squares = Array.new  
horiz\_segments.combination(2) do |side, candidate|  
if (…whatever…)  
squares \<\< Square.new(side, candidate)  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![Ruhe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruhe](https://www.ruby-forum.com/u/Ruhe)\
**Post date:** [February 16, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/iterate-through-array-twice/129310/4 "2008-02-16T20:20:34Z")

</div>

Yes, Ruby is the most beautiful language!
