J verb or phrase to group together runs of identical elements

Viewed 93

This is best demonstrated by example:

Input:

1 2 2 1 1 1 2 3 3

Output:

    1; 2 2; 1 1 1; 2; 3 3

┌─┬───┬─────┬─┬───┐
│1│2 2 │1 1 1│2│3 3│
└─┴───┴─────┴─┴───┘

So I'm looking for something like Key /., but Key would group all ones together, all twos together, and so on. I want to group only contiguous runs together.

One final thought: Key can be used successfully, if I can first generate an x for it that looks like this:

0 1 1 2 2 2 3 4 4   NB. x suitable for use in Key
1 2 2 1 1 1 2 3 3   NB. original data

I cannot think of a tacit solution for generating such an x, though (my ideas involve a mutating temporary variable to store the current "label", ie, the numbers 0 through 4 in the x defined above)

1 Answers
Related