c# - How to select non-distinct elements along with their indexes -


 list<string> str = new list<string>() {    "alpha", "beta", "alpha", "alpha", "gamma", "beta", "xyz" }; 

expected output:

 string | indexes  ----------------------------  alpha  | 0, 2, 3  beta   | 1, 5 

gamma , xyz distinct so, ignored.

i've done comparing strings manually. possible using linq in more easier way?

foreach (var grp in    str.select((s, i) => new { s, })       .tolookup(pair => pair.s, pair => pair.i)       .where(pair => pair.count() > 1)) {        console.writeline("{0}: {1}", grp.key, string.join(", ", grp)); } 

Comments