How to combine equal sequence elements (functional programming)?

后端 未结 4 1934
情书的邮戳
情书的邮戳 2021-01-20 15:42

I want to write a function that takes in a sequence <1,1,2,2,3> and returns the sequence with equal elements grouped like <<1,1>, <2,2>, <3>>.

I\'m u

4条回答
  •  野的像风
    2021-01-20 16:10

    Don't know about the languages you are using, but from functional perspective, I would:

    1. take sequence of distinct values
    2. map each value in distinct sequence to filtered original sequence.

    Pseudo code:

    originalSequence = <1,1,2,2,3>
    distinctSequence = originalSequnce.distinct() // <1,2,3>
    result = distinctSequence.map(elem => originalSequence.filter(e == elem)) // <<1,1>, <2, 2>, <3>>
    

提交回复
热议问题