Horje
C# Program To Remove Duplicates From A Given String

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 = horje
Output: geksfor
Explanation: After removing duplicate characters such as e, k, g, s, we have string as “geksfor”.

Input: Str = HappyNewYear
Output: HapyNewYr
Explanation: 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 = "horje".ToCharArray();
        int n = str.Length;
        Console.WriteLine(removeDuplicate(str, n));
    }
}
 
// This code is contributed by PrinciRaj1992

Output:  

geksfor

Time Complexity : O(n * n) 
Auxiliary Space : O(1) , Keeps order of elements the same as input. 

Remove duplicates from a given string using Hashing

Iterating 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 = "horje".ToCharArray();
        int n = s.Length;
        Console.Write(removeDuplicates(s, n));
    }
}

Output:  

geksfor

Time Complexity: O(n)
Auxiliary Space: O(n)

Please refer complete article on Remove duplicates from a given string for more details!




Reffered: https://www.geeksforgeeks.org


C Programs

Related
C Program for Depth First Search or DFS for a Graph C Program for Depth First Search or DFS for a Graph
Array of Linked Lists in C/C++ Array of Linked Lists in C/C++
C Program For Pairwise Swapping Elements Of A Given Linked List By Changing Links C Program For Pairwise Swapping Elements Of A Given Linked List By Changing Links
C Program For Union And Intersection Of Two Linked Lists C Program For Union And Intersection Of Two Linked Lists
All forms of formatted scanf() in C All forms of formatted scanf() in C

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13