I'm looking for an elegant way to combine every element of a Seq
with the rest for a large collection.
Example: Seq(1,2,3).someMethod
should produce something like
Iterator(
(1,Seq(2,3)),
(2,Seq(1,3)),
(3,Seq(1,2))
)
Order of elements doesn't matter. It doesn't have to be a tuple, a Seq(Seq(1),Seq(2,3))
is also acceptable (although kinda ugly).
Note the emphasis on large collection (which is why my example shows an Iterator
).
Also note that this is not combinations
.
Ideas?
Edit: In my use case, the numbers are expected to be unique. If a solution can eliminate the dupes, that's fine, but not at additional cost. Otherwise, dupes are acceptable.
Edit 2: In the end, I went with a nested for
-loop, and skipped the case when i == j
. No new collections were created. I upvoted the solutions that were correct and simple ("simplicity is the ultimate sophistication" - Leonardo da Vinci), but even the best ones are quadratic just by the nature of the problem, and some create intermediate collections by usage of ++
that I wanted to avoid because the collection I'm dealing with has close to 50000 elements, 2.5 billion when quadratic.