How to remove duplicate characters in a string?

I need to implement a function that takes a string as input and finds a non-double character from this string.

So, an example: if I pass the string str = "DHCD", it will return "DHC" or str2 = "KLKLHHMO" it will return "KLHMO"

+10
c #
source share
20 answers

Linq approach:

public static string RemoveDuplicates(string input) { return new string(input.ToCharArray().Distinct().ToArray()); } 
+32
source share

He will do the work

 string removedupes(string s) { string newString = string.Empty; List<char> found = new List<char>(); foreach(char c in s) { if(found.Contains(c)) continue; newString+=c.ToString(); found.Add(c); } return newString; } 

I must note that this is criminally ineffective.

I think I was delirious in the first revision.

+8
source share

For arbitrary character string lengths of byte size (not for wide characters or other encodings) I would use a lookup table, one bit per character (32 bytes for a 256-bit table). Scroll the line, only output characters that do not have their own bits, then turn on the bit for that character.

 string removedupes(string s) { string t; byte[] found = new byte[256]; foreach(char c in s) { if(!found[c]) { t.Append(c); found[c]=1; } } return t; } 

I am poorly versed in C #, so I don’t know how to use a bit field instead of an array of bytes.

If you know that your lines will be very short, then other approaches will make better use of memory and / or speed.

+6
source share
  void removeDuplicate() { string value1 = RemoveDuplicateChars("Devarajan"); } static string RemoveDuplicateChars(string key) { string result = ""; foreach (char value in key) if (result.IndexOf(value) == -1) result += value; return result; } 
+4
source share

It sounds like homework to me, so I’ll just describe it at a high level.

  • Scroll through the line, exploring each character
  • Check if you saw the character before
    • if you have, remove it from the line
    • If you haven’t done so, please note that you have already seen this symbol.
+3
source share

My answer is in Java.
Held here so you can get an idea, even if it is in Java. The algorithm will remain the same.

 public String removeDup(String s) { if(s==null) return null; int l = s.length(); //if length is less than 2 return string if(l<2)return s; char arr[] = s.toCharArray(); for(int i=0;i<l;i++) { int j =i+1; //index to check with ith index int t = i+1; //index of first repetative char. while(j<l) { if(arr[j]==arr[i]) { j++; } else { arr[t]=arr[j]; t++; j++; } } l=t; } return new String(arr,0,l); } 
+1
source share

// this is in C #, validation is left for brevity // a primitive solution to remove duplicate characters from a given string

  public static char[] RemoveDup(string s) { char[] c = new char[s.Length]; int unique = 0; c[unique] = s[0]; // Assume: First char is trivial for (int i = 1; i < s.Length; i++) { if (s[i-1] != s[i] c[++unique] = s[i]; } return c; } 
+1
source share

you can use hashset:

  static void Main() { string textWithDuplicates = "aaabbcccggg"; Console.WriteLine(textWithDuplicates.Count()); var letters = new HashSet<char>(textWithDuplicates); Console.WriteLine(letters.Count()); foreach (char c in letters) Console.Write(c); } 
+1
source share

char * remove_duplicates (char * str) {char * str1, * str2;

 if(!str) return str; str1 = str2 = str; while(*str2) { if(strchr(str, *str2)<str2) { str2++; continue; } *str1++ = *str2++; } *str1 = '\0'; return str; 

}

0
source share
 char* removeDups(const char* str) { char* new_str = (char*)malloc(256*sizeof(char)); int i,j,current_pos = 0,len_of_new_str; new_str[0]='\0'; for(i=0;i<strlen(str);i++) { len_of_new_str = strlen(new_str); for(j=0;j<len_of_new_str && new_str[j]!=str[i];j++) ; if(j==len_of_new_str) { new_str[len_of_new_str] = str[i]; new_str[len_of_new_str+1] = '\0'; } } return new_str; } 

Hope this helps

0
source share
 String str="AABBCANCDE"; String newStr=""; for( int i=0; i<str.length(); i++) { if(!newStr.contains(str.charAt(i)+"")) newStr= newStr+str.charAt(i); } System.out.println(newStr); 
0
source share

// Remove Both Upper Bottom Duplicates

 public static string RemoveDuplicates(string key) { string Result = string.Empty; foreach (char a in key) { if (Result.Contains(a.ToString().ToUpper()) || Result.Contains(a.ToString().ToLower())) continue; Result += a.ToString(); } return Result; } 
0
source share
  class Program { static void Main(string[] args) { bool[] doesExists = new bool[256]; String st = Console.ReadLine(); StringBuilder sb = new StringBuilder(); foreach (char ch in st) { if (!doesExists[ch]) { sb.Append(ch); doesExists[ch] = true; } } Console.WriteLine(sb.ToString()); } } 
0
source share
 Console.WriteLine("Enter String"); string str = Console.ReadLine(); string result = ""; result += str[0]; // first character of string for (int i = 1; i < str.Length; i++) { if (str[i - 1] != str[i]) result += str[i]; } Console.WriteLine(result); 
0
source share

I like Quintin Robinson's answer, only there should be some improvements, such as deleting the list, because in this case it is not necessary. In addition, in my opinion, the Uppercase char ("K") and the lowercase char ("k") are the same thing, so they should be considered the same.

So here is how I do it:

 private static string RemoveDuplicates(string textEntered) { string newString = string.Empty; foreach (var c in textEntered) { if (newString.Contains(char.ToLower(c)) || newString.Contains(char.ToUpper(c))) { continue; } newString += c.ToString(); } return newString; } 
0
source share

Not sure how optimal this is:

 public static string RemoveDuplicates(string input) { var output = string.Join("", input.ToHashSet()); return output; } 
0
source share

Below is the code to remove duplicate characters from a string

  var input = "SaaSingeshe"; var filteredString = new StringBuilder(); foreach(char c in input) { if(filteredString.ToString().IndexOf(c)==-1) { filteredString.Append(c); } } Console.WriteLine(filteredString); Console.ReadKey(); 
0
source share

Demonstration of the namespace {class Program {

  static void Main(string[] args) { string myStr = "kkllmmnnouo"; Console.WriteLine("Initial String: "+myStr); // var unique = new HashSet<char>(myStr); HashSet<char> unique = new HashSet<char>(myStr); Console.Write("New String after removing duplicates: "); foreach (char c in unique) Console.Write(c); } } } 
0
source share

revised version of first answer in C # below

string oldstr = "abacab";

string newstr = new string (oldstr.Distinct (). ToArray ());

-one
source share
 var input1 = Console.ReadLine().ToLower().ToCharArray(); var input2 = input1; var WithoutDuplicate = input1.Union(input2); 
-one
source share

All Articles