Skip to content

Instantly share code, notes, and snippets.

@dirkgr
Created November 30, 2018 23:39
Show Gist options
  • Save dirkgr/2f85e71718eb1abb923401e2566c334a to your computer and use it in GitHub Desktop.
Save dirkgr/2f85e71718eb1abb923401e2566c334a to your computer and use it in GitHub Desktop.
distinct iterator
def distinctFromIterator[T](input: Iterator[T]): Iterator[T] = new Iterator[T] {
private val seen: mutable.Set[T] = mutable.Set[T]()
private def findNextItem(): Option[T] = {
if(input.hasNext) {
val n = input.next()
val newItem = seen.add(n)
if(newItem)
Some(n)
else
findNextItem()
} else {
None
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment