c# - Allow only a set number of characters in a string -


i working on function evaluate string , allow x instances of each character.

for example, can have 2 allowed character so

aaaabbbbbcddddd 

would evaluated

aabbcdd 

so far have written this:

public static string removeduplicatecharacters(string text, int allowedduplicates) {     string seen="";     foreach (char c in text){         if(!seen.contains(c)){            seen = seen + c;        } else if(seen.contains(c)){               // while sting contains < allowedduplicates             // add c            }        }     return seen;   }       

i can't @ moment work out how create while condition going count through seen string number of current instances of char being evaluated.

easy solution dictionary keep track of character counts:

public static string removeduplicatecharacters(string text, int allowedduplicates) {     string seen="";     dictionary<char, int> charcount = new   dictionary<char, int>();     foreach (char c in text)     {         if(!charcount.containskey(c))         {             seen += c;             charcount.add(c, 1);         }         else if(charcount[c] < allowedduplicates)         {             charcount[c] += 1;             seen += c;         }         else         {             //reached max, nothing         }     }     return seen;   }       

this base , can make nice , fancy want here.

e.g.: suggest stringbuilder if strings can long less taxing on memory since don't have allocate permanently new strings when doing += on them.

public static string removeduplicatecharacters(string text, int allowedduplicates) {     stringbuilder seen = new stringbuilder();      dictionary<char, int> charcount = new   dictionary<char, int>();     foreach (char c in text)     {         if(!charcount.containskey(c))         {             seen.append(c);             charcount.add(c, 1);         }         else if(charcount[c] < allowedduplicates)         {             charcount[c] += 1;             seen.append(c);         }         else         {             //reached max, nothing         }     }     return seen.tostring();   }      

another thing if want lower , uppercase treated same. change test upper or lower case, if want keep casing of original character in return string following.

public static string removeduplicatecharacters(string text, int allowedduplicates) {     stringbuilder seen = new stringbuilder();      dictionary<char, int> charcount = new   dictionary<char, int>();     foreach (char c in text)     {         char uppercase = c.toupper();         if(!charcount.containskey(uppercase))         {             seen.append(c);             charcount.add(uppercase , 1);         }         else if(charcount[uppercase] < allowedduplicates)         {             charcount[uppercase ] += 1;             seen.append(c);         }         else         {             //reached max, nothing         }     }     return seen.tostring();   }       

just customize here on.


Comments