Here are some examples:
[1, 2, 3, 4, 5] => [1, 2, 3, 4, 5]
[10, 15, 10, 15, 30] => [10, 15]
[1, 2, 3, 4, 1, 5, 6, 7] => [1, 2, 3, 4]
Here's my best (and deeply ugly) non-recursive, side-effect-free solution so far:
x.scanLeft(List[Int]())((B, Term) => Term :: B).drop(1).takeWhile(i => !(i.tail contains i.head)).last.reverse
Minor optimization:
x.tail.scanLeft(List(x.head))((B, Term) => Term :: B).takeWhile(i => !(i.tail contains i.head)).last.reverse
This is different from distinct
:
[1, 2, 3, 4, 1, 5, 6, 7] => [1, 2, 3, 4] and not [1, 2, 3, 4, 5, 6, 7]
Also, considering List[_]
is a monoid, isn't there a scan
that uses the monoid zero
?
distinct
method... see stackoverflow.com/questions/1538598/… \$\endgroup\$