Remove duplicates from a given string
Last Updated :
20 Sep, 2023
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: Str = geeksforgeeks
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++
#include <bits/stdc++.h>
using namespace std;
char *removeDuplicate( char str[], int n)
{
int index = 0;
for ( int i=0; i<n; i++) {
int j;
for (j=0; j<i; j++)
if (str[i] == str[j])
break ;
if (j == i)
str[index++] = str[i];
}
return str;
}
int main()
{
char str[]= "geeksforgeeks" ;
int n = sizeof (str) / sizeof (str[0]);
cout << removeDuplicate(str, n);
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
char * removeDuplicate( char str[], int n)
{
int index = 0;
for ( int i = 0; i < n; i++) {
int j;
for (j = 0; j < i; j++) {
if (str[i] == str[j])
break ;
}
if (j == i)
str[index++] = str[i];
}
str[index] = '\0' ;
return str;
}
int main()
{
char str[] = "geeksforgeeks" ;
int n = sizeof (str) / sizeof (str[0]);
printf ( "%s\n" , removeDuplicate(str, n));
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String removeDuplicate( char str[], int n)
{
int index = 0 ;
for ( int i = 0 ; i < n; i++)
{
int j;
for (j = 0 ; j < i; j++)
{
if (str[i] == str[j])
{
break ;
}
}
if (j == i)
{
str[index++] = str[i];
}
}
return String.valueOf(Arrays.copyOf(str, index));
}
public static void main(String[] args)
{
char str[] = "geeksforgeeks" .toCharArray();
int n = str.length;
System.out.println(removeDuplicate(str, n));
}
}
|
Python3
string = "geeksforgeeks"
p = ""
for char in string:
if char not in p:
p = p + char
print (p)
k = list ( "geeksforgeeks" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static String removeDuplicate( char []str, int n)
{
int index = 0;
for ( int i = 0; i < n; i++)
{
int j;
for (j = 0; j < i; j++)
{
if (str[i] == str[j])
{
break ;
}
}
if (j == i)
{
str[index++] = str[i];
}
}
char [] ans = new char [index];
Array.Copy(str, ans, index);
return String.Join( "" , ans);
}
public static void Main(String[] args)
{
char []str = "geeksforgeeks" .ToCharArray();
int n = str.Length;
Console.WriteLine(removeDuplicate(str, n));
}
}
|
Javascript
<script>
function removeDuplicate(str, n)
{
var index = 0;
for ( var i = 0; i < n; i++)
{
var j;
for (j = 0; j < i; j++)
{
if (str[i] == str[j])
{
break ;
}
}
if (j == i)
{
str[index++] = str[i];
}
}
return str.join( "" ).slice(str, index);
}
var str = "geeksforgeeks" .split( "" );
var n = str.length;
document.write(removeDuplicate(str, n));
</script>
|
Time Complexity: O(n * n)
Auxiliary Space: O(1), Keeps the order of elements the same as the 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++
#include <bits/stdc++.h>
using namespace std;
string removeDuplicates(string s, int n)
{
unordered_map< char , int > exists;
string ans = "" ;
for ( int i = 0; i < n; i++) {
if (exists.find(s[i]) == exists.end()) {
ans.push_back(s[i]);
exists[s[i]]++;
}
}
return ans;
}
int main()
{
string s = "geeksforgeeks" ;
int n = s.size();
cout << removeDuplicates(s, n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static char [] removeDuplicates( char []s, int n){
Map<Character,Integer> exists = new HashMap<>();
String st = "" ;
for ( int i = 0 ; i < n; i++){
if (!exists.containsKey(s[i]))
{
st += s[i];
exists.put(s[i], 1 );
}
}
return st.toCharArray();
}
public static void main(String[] args){
char s[] = "geeksforgeeks" .toCharArray();
int n = s.length;
System.out.print(removeDuplicates(s,n));
}
}
|
Python3
def removeDuplicates(s, n):
exists = {}
index = 0
ans = ""
for i in range ( 0 , n):
if s[i] not in exists or exists[s[i]] = = 0 :
s[index] = s[i]
print (s[index], end = '')
index + = 1
exists[s[i]] = 1
s = "geeksforgeeks"
s1 = list (s)
n = len (s1)
removeDuplicates(s1, n)
|
C#
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();
}
public static void Main(String[] args){
char []s = "geeksforgeeks" .ToCharArray();
int n = s.Length;
Console.Write(removeDuplicates(s,n));
}
}
|
Javascript
<script>
function removeDuplicates( s , n) {
var exists = new Map();
var st = "" ;
for ( var i = 0; i < n; i++) {
if (!exists.has(s[i])) {
st += s[i];
exists.set(s[i], 1);
}
}
return st;
}
var s = "geeksforgeeks" ;
var n = s.length;
document.write(removeDuplicates(s, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...