The problem is caused by modifying the array (with delete
) while you iterate over that array.
In the first iteration the iterator "points" to the first element in the array. Since you delete that first element, the second element becomes the first. In the next iteration the iterator points to the second element in the array - the one that was the third in the first iteration. You skip the element that was the second in the first iteration and move to the front.
As you see you would not check each element instead you only check every second element.
It might be a bit more efficient to sort the array first and then only check each consecutive elements if they are equal:
array.sort.each_cons(2) { |x, y| puts(x) if x == y }