Simple way to group the list-indices of equal elements?
I would do it like this
Values[PositionIndex[list]]
You can use the ResourceFunction
GroupByList
to do this:
list = {1, 2, 3, 3};
ResourceFunction["GroupByList"][Range @ Length @ list, list]
<|1 -> {1}, 2 -> {2}, 3 -> {3, 4}|>
list = {f, f, g, g, f};
ResourceFunction["GroupByList"][Range @ Length @ list, list]
<|f -> {1, 2, 5}, g -> {3, 4}|>
Use Values
to convert the Association
to a list.
You can also used Reap
/Sow
, e.g.
pi[u_] := Reap[MapIndexed[Sow[#2[[1]], #1] &, u]][[2]]
So pi[{f, f, g, g, f}]
yields
{{1, 2, 5}, {3, 4}}