DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Aniruddha has posted 40 posts at DZone. View Full User Profile

Remove Duplicate Characters - C# (Case Sensitive)

05.16.2012
| 5375 views |
  • submit to reddit
Func<string, string> RemoveDuplicate = delegate(string s)
            {
                BitArray _arr = new BitArray(256);
                StringBuilder _sb = new StringBuilder();
                s = s.ToLower();
                for (int i = 0; i < s.Length; i++)
                {
                    if (_arr[(int)s[i]])
                    {
                        continue;
                    }
                    else
                    {
                        _arr[(int)s[i]] = true;
                        _sb.Append(s[i]);
                    }
                }
                return _sb.ToString();
            };

Remove Duplicate characters from string using BitArray.

This is case-sensitive removal.