Search code examples
j

How to group equal values in J?


   (= <@#"1 0 ~.) 3 2 3 3 4
+-----+-+-+
|3 3 3|2|4|
+-----+-+-+

What other ways are there? I mostly don’t like the part where it says "1 0.


Solution

  • The adverb key (/.) groups the right argument as specified by the left argument and applies a verb to each group. If you give it the same left and right argument you can apply the verb box (<) to group all the equal items

       </.~ 3 2 3 3 4
    ┌─────┬─┬─┐
    │3 3 3│2│4│
    └─────┴─┴─┘
    

    You could easily sum the groups instead:

       +//.~ 3 2 3 3 4
    9 2 4