Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping adjacent elements in a list

Let's say I want to write a function that does this:

input: [1,1,3,3,4,2,2,5,6,6] output: [[1,1],[3,3],[4],[2,2],[5],[6,6]]

It's grouping adjacent elements that are same.

What should the name of this method be? Is there a standard name for this operation?

like image 202
user855 Avatar asked Dec 11 '22 14:12

user855


1 Answers

In [1,1,3,3,4,2,2,5,6,6], a thing like [1,1] is very often referred to as run (as in run-length encoding, see RLE in Scala). I'd therefore call the method groupRuns.

like image 113
gzm0 Avatar answered Jan 04 '23 10:01

gzm0