C# Program To Remove Duplicates From A Given String Last Updated : 13 Oct, 2023 Comments Improve Suggest changes Like Article Like Report Write a C# program for a given string S which may contain lowercase and uppercase characters. The task is to remove all duplicate characters from the string and find the resultant string. Note: The order of remaining characters in the output should be the same as in the original string.Example: Input: Str = geeksforgeeksOutput: geksforExplanation: After removing duplicate characters such as e, k, g, s, we have string as “geksfor”. Input: Str = HappyNewYearOutput: HapyNewYrExplanation: After removing duplicate characters such as p, e, a, we have string as “HapyNewYr”. Naive Approach: Iterate through the string and for each character check if that particular character has occurred before it in the string. If not, add the character to the result, otherwise the character is not added to result. Below is the implementation of above approach: C# // C# program to remove duplicate character // from character array and print in sorted // order using System; using System.Collections.Generic; class GFG { static String removeDuplicate(char[] str, int n) { // Used as index in the modified string int index = 0; // Traverse through all characters for (int i = 0; i < n; i++) { // Check if str[i] is present before it int j; for (j = 0; j < i; j++) { if (str[i] == str[j]) { break; } } // If not present, then add it to // result. if (j == i) { str[index++] = str[i]; } } char[] ans = new char[index]; Array.Copy(str, ans, index); return String.Join("", ans); } // Driver code public static void Main(String[] args) { char[] str = "geeksforgeeks".ToCharArray(); int n = str.Length; Console.WriteLine(removeDuplicate(str, n)); } } // This code is contributed by PrinciRaj1992 Output: geksforTime Complexity : O(n * n) Auxiliary Space : O(1) , Keeps order of elements the same as input. Remove duplicates from a given string using HashingIterating through the given string and use a map to efficiently track of encountered characters. If a character is encountered for the first time, it’s added to the result string, Otherwise, it’s skipped. This ensures the output string contains only unique characters in the same order as the input string. Below is the implementation of above approach: C# // C# program to create a unique String using unordered_map /* access time in unordered_map on is O(1) generally if no collisions occur and therefore it helps us check if an element exists in a String in O(1) time complexity with constant space. */ using System; using System.Collections.Generic; public class GFG { static char[] removeDuplicates(char[] s, int n) { Dictionary<char, int> exists = new Dictionary<char, int>(); String st = ""; for (int i = 0; i < n; i++) { if (!exists.ContainsKey(s[i])) { st += s[i]; exists.Add(s[i], 1); } } return st.ToCharArray(); } // driver code public static void Main(String[] args) { char[] s = "geeksforgeeks".ToCharArray(); int n = s.Length; Console.Write(removeDuplicates(s, n)); } } Output: geksforTime Complexity: O(n)Auxiliary Space: O(n) Please refer complete article on Remove duplicates from a given string for more details! Comment More infoAdvertise with us Next Article C# Program To Remove Duplicates From A Given String kartik Follow Improve Article Tags : Strings C Programs DSA frequency-counting Practice Tags : Strings Similar Reads C Program to Remove Duplicates from Sorted Array In this article, we will learn how to remove duplicates from a sorted array using the C program.The most straightforward method is to use the two-pointer approach which uses two pointers: one pointer to iterate over the array and other to track duplicate elements. In sorted arrays, duplicates are ad 4 min read C Program to Compare Two Strings Using Pointers In C, two strings are generally compared character by character in lexicographical order (alphabetical order). In this article, we will learn how to compare two strings using pointers.To compare two strings using pointers, increment the pointers to traverse through each character of the strings whil 2 min read How to Create a Dynamic Array of Strings in C? In C, dynamic arrays are essential for handling data structures whose size changes dynamically during the program's runtime. Strings are arrays of characters terminated by the null character '\0'. A dynamic array of strings will ensure to change it's size dynamically during the runtime of the progra 3 min read C Program to Compare Two Strings Without Using strcmp() String comparison refers to the process of comparing two strings to check if they are equal or determine their lexicographical order. C provides the strcmp() library function to compare two strings but in this article, we will learn how to compare two strings without using strcmp() function.The most 2 min read Modify array of strings by replacing characters repeating in the same or remaining strings Given an array of strings arr[] consisting of lowercase and uppercase characters only, the task is to modify the array by removing the characters from the strings which are repeating in the same string or any other string. Print the modified array. Examples: Input: arr[] = {"Geeks", "For", "Geeks"}O 6 min read Check if all occurrences of a character appear together Given a string s and a character c, find if all occurrences of c appear together in s or not. If the character c does not appear in the string at all, the answer is true. Examples Input: s = "1110000323", c = '1' Output: Yes All occurrences of '1' appear together in "1110000323" Input: s = "3231131" 7 min read C++ Program To Remove Duplicates From Sorted Array Given a sorted array, the task is to remove the duplicate elements from the array.Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} new size = 1 Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5} new size = 5 Recommended PracticeRemove duplicate elements from sorte 3 min read Remove duplicates from a string Given a string s which may contain lowercase and uppercase characters. The task is to remove all duplicate characters from the string and find the resultant string. Note: The order of remaining characters in the output should be the same as in the original string.Example:Input: s = geeksforgeeksOutp 10 min read Remove duplicates from a string in O(1) extra space Given a string str of lowercase characters, the task is to remove duplicates and return a resultant string without modifying the order of characters in the original string. Examples: Input: str = "geeksforgeeks" Output: geksfor Input: str = "characters" Output: chartes Approach: The idea is to use b 13 min read Remove all consecutive duplicates from the string Given a string s , The task is to remove all the consecutive duplicate characters of the string and return the resultant string. Examples: Input: s = "aaaaabbbbbb"Output: abExplanation: Remove consecutive duplicate characters from a string s such as 5 a's are at consecative so only write a and same 10 min read Like