Try with foldLeft() :
val input = List(1, 2, 3, 4, 5, 6, 7, 11, 20) input.tail.foldLeft(List(input.head))((out, cur) => if(cur - out.head >= 5) cur :: out else out ).reverse
If this is not obvious:
The algorithm starts with the first element (maybe you need processed faces) in the output collection
Iterates over all remaining elements from input . If the difference between this ( cur ) element and the first input element is greater than or equal to 5 , add to input . Otherwise, skip and continue
input was created by adding and exploring head to improve performance. .reverse required at the end
This is basically how you should implement this for real, but with more concise syntax.
source share