c# - Make every string unique by appending counter -


i have list of string , want make every string in list unique appending number @ end of it. also, case insensitive, "apple" should assumed same "apple" or "apple"

for example:

list<string> input = new list<string>(); input.add("apple"); input.add("ball"); input.add("apple"); input.add("apple"); input.add("car"); input.add("ball"); input.add("ball"); 

expected output:

"apple", "ball", "apple2", "apple3", "car", "ball2", "ball3"

i need develop logic produce output. thank you.

edited: cannot have 0 , 1, repeated string must start 2, 3, 4...

var newlist = input.groupby(x => x.toupper())               .selectmany(g => g.select((s, i) => == 0 ? s : s + (i+1)))               .tolist();   var str = string.join(", ", newlist); 

edit

var newlist = input.select((s, i) => new { str = s, orginx = })                 .groupby(x => x.str.toupper())                 .select(g => g.select((s, j) => new { s = j == 0 ? s.str : s.str + (j + 1), s.orginx }))                 .selectmany(x => x)                 .orderby(x => x.orginx)                 .select(x => x.s)                 .tolist(); 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -