Remove duplicates from a string
Last Updated :
11 Sep, 2024
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 = geeksforgeeks
Output: geksfor
Explanation: After removing duplicate characters such as e, k, g, s, we have string as "geksfor".
Input: s = HappyNewYear
Output: HapyNewYr
Explanation: After removing duplicate characters such as p, e, a, we have string as "HapyNewYr".
Naive Approach - O(n^2) Time
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;
string removeDuplicate(string s)
{
// Used as index in the modified string
int index = 0;
// Traverse through all characters
for (int i = 0; i < s.size(); i++) {
// Check if s[i] is present before it
int j;
for (j = 0; j < i; j++)
if (s[i] == s[j])
break;
// If not present, then add it to result
if (j == i)
s[index++] = s[i];
}
// Resize the string to remove extra characters
s.resize(index);
return s;
}
// Driver code
int main()
{
string s = "geeksforgeeks";
cout << removeDuplicate(s);
return 0;
}
C
#include <stdio.h>
#include <string.h>
// Used as index in the modified string
char* removeDuplicate(char* s) {
int index = 0;
int i, j;
int len = strlen(s);
// Traverse through all characters
for (i = 0; i < len; i++) {
// Check if s[i] is present before it
for (j = 0; j < i; j++)
if (s[i] == s[j])
break;
// If not present, then add it to result
if (j == i)
s[index++] = s[i];
}
// Resize the string to remove extra characters
s[index] = '\0';
return s;
}
// Driver code
int main() {
char s[] = "geeksforgeeks";
printf("%s\n", removeDuplicate(s));
return 0;
}
Java
public class GfG {
// Used as index in the modified string
public static String removeDuplicate(String s) {
StringBuilder sb = new StringBuilder(s.length());
boolean[] seen = new boolean[256];
// Traverse through all characters
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
// Check if s[i] is present before it
if (!seen[c]) {
sb.append(c);
seen[c] = true;
}
}
return sb.toString();
}
// Driver code
public static void main(String[] args) {
String s = "geeksforgeeks";
System.out.println(removeDuplicate(s));
}
}
Python
def remove_duplicate(s):
# Used as index in the modified string
result = []
seen = set()
# Traverse through all characters
for char in s:
# Check if s[i] is present before it
if char not in seen:
result.append(char)
seen.add(char)
# Join list to form the result string
return ''.join(result)
# Driver code
s = "geeksforgeeks"
print(remove_duplicate(s))
C#
using System;
public class GfG
{
// Used as index in the modified string
public static string RemoveDuplicateChars(string s)
{
char[] arr = s.ToCharArray();
int index = 0;
// Traverse through all characters
for (int i = 0; i < arr.Length; i++)
{
// Check if s[i] is present before it
int j;
for (j = 0; j < i; j++)
{
if (arr[i] == arr[j])
break;
}
// If not present, then add it to result
if (j == i)
arr[index++] = arr[i];
}
// Resize the string to remove extra characters
return new string(arr, 0, index);
}
// Driver code
public static void Main()
{
string s = "geeksforgeeks";
Console.WriteLine(RemoveDuplicateChars(s));
}
}
JavaScript
function removeDuplicate(s) {
// Used as index in the modified string
let result = '';
let seen = new Set();
// Traverse through all characters
for (let i = 0; i < s.length; i++) {
let char = s[i];
// Check if s[i] is present before it
if (!seen.has(char)) {
result += char;
seen.add(char);
}
}
return result;
}
// Driver code
let s = "geeksforgeeks";
console.log(removeDuplicate(s));
Time Complexity: O(n * n)
Auxiliary Space: O(1), Keeps the order of elements the same as the input.
Using Hash Set - O(n) Time
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;
// Function to remove duplicate characters
// using unordered_set
string removeDuplicates(string &s)
{
unordered_set<char> exists;
string ans = "";
// Traverse through the string
for (char c : s) {
// If character is not found in set,
// add it to result
if (exists.find(c) == exists.end()) {
ans.push_back(c);
exists.insert(c);
}
}
return ans;
}
// Driver code
int main()
{
string s = "geeksforgeeks";
cout << removeDuplicates(s) << endl;
return 0;
}
C
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// Function to remove duplicate characters
// using an array to track characters
char* removeDuplicates(char* s) {
bool exists[256] = { false };
int index = 0;
int length = strlen(s);
// Traverse through the string
for (int i = 0; i < length; i++) {
char c = s[i];
// If character is not found in array,
// add it to result
if (!exists[(unsigned char)c]) {
s[index++] = c;
exists[(unsigned char)c] = true;
}
}
// Null-terminate the result string
s[index] = '\0';
return s;
}
// Driver code
int main() {
char s[] = "geeksforgeeks";
printf("%s\n", removeDuplicates(s));
return 0;
}
Java
import java.util.HashSet;
public class GfG {
// Function to remove duplicate characters
// using HashSet
public static String removeDuplicates(String s) {
HashSet<Character> exists = new HashSet<>();
StringBuilder ans = new StringBuilder();
// Traverse through the string
for (char c : s.toCharArray()) {
// If character is not found in set,
// add it to result
if (!exists.contains(c)) {
ans.append(c);
exists.add(c);
}
}
return ans.toString();
}
// Driver code
public static void main(String[] args) {
String s = "geeksforgeeks";
System.out.println(removeDuplicates(s));
}
}
Python
def remove_duplicates(s):
# Function to remove duplicate characters
# using set
seen = set()
result = []
# Traverse through the string
for char in s:
# If character is not found in set,
# add it to result
if char not in seen:
result.append(char)
seen.add(char)
return ''.join(result)
# Driver code
s = "geeksforgeeks"
print(remove_duplicates(s))
C#
using System;
using System.Collections.Generic;
public class GfG {
// Function to remove duplicate characters
// using HashSet
public static string RemoveDuplicatesChars(string s) {
HashSet<char> exists = new HashSet<char>();
System.Text.StringBuilder ans = new System.Text.StringBuilder();
// Traverse through the string
foreach (char c in s) {
// If character is not found in set,
// add it to result
if (!exists.Contains(c)) {
ans.Append(c);
exists.Add(c);
}
}
return ans.ToString();
}
// Driver code
public static void Main() {
string s = "geeksforgeeks";
Console.WriteLine(RemoveDuplicatesChars(s));
}
}
JavaScript
function removeDuplicates(s) {
// Function to remove duplicate characters
// using a Set
let seen = new Set();
let result = '';
// Traverse through the string
for (let char of s) {
// If character is not found in set,
// add it to result
if (!seen.has(char)) {
result += char;
seen.add(char);
}
}
return result;
}
// Driver code
let s = "geeksforgeeks";
console.log(removeDuplicates(s));
Time Complexity: O(n)
Auxiliary Space: O(n)
Using Frequency Array - O(n)
Iterating through the given string and use a character array initilize with 0 frequency to efficiently track of encountered characters. If current character's frequency is 0, then it's added to the result string and increment frequency by 1, 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;
// Function to remove duplicate characters
string removeDuplicates(string &s)
{
// Create an integer array to store
// frequency for ASCII characters
vector<int> ch(256, 0);
// Create result string
string ans = "";
// Traverse the input string
for (char c : s) {
// Check if current character's frequency is 0
if (ch[c] == 0) {
// Add char if frequency is 0
ans.push_back(c);
// Increment frequency
ch[c]++;
}
}
return ans;
}
// Driver code
int main()
{
string s = "geeksforgeeks";
cout << removeDuplicates(s) << endl;
return 0;
}
C
#include <stdio.h>
#include <string.h>
// Function to remove duplicate characters
char* removeDuplicates(char* s) {
// Create an integer array to store
// frequency for ASCII characters
int ch[256] = { 0 };
int index = 0;
int length = strlen(s);
// Traverse the input string
for (int i = 0; i < length; i++) {
char c = s[i];
// Check if current character's frequency is 0
if (ch[(unsigned char)c] == 0) {
// Add char if frequency is 0
s[index++] = c;
// Increment frequency
ch[(unsigned char)c]++;
}
}
s[index] = '\0'; // Null-terminate the result string
return s;
}
// Driver code
int main() {
char s[] = "geeksforgeeks";
printf("%s\n", removeDuplicates(s));
return 0;
}
Java
import java.util.Arrays;
public class GfG {
// Function to remove duplicate characters
public static String removeDuplicates(String s) {
// Create an integer array to store
// frequency for ASCII characters
int[] ch = new int[256];
StringBuilder ans = new StringBuilder();
// Traverse the input string
for (char c : s.toCharArray()) {
// Check if current character's frequency is 0
if (ch[c] == 0) {
// Add char if frequency is 0
ans.append(c);
// Increment frequency
ch[c]++;
}
}
return ans.toString();
}
// Driver code
public static void main(String[] args) {
String s = "geeksforgeeks";
System.out.println(removeDuplicates(s));
}
}
Python
def remove_duplicates(s):
# Function to remove duplicate characters
# Create a list to store frequency for ASCII characters
ch = [0] * 256
result = []
# Traverse the input string
for char in s:
# Check if current character's frequency is 0
if ch[ord(char)] == 0:
# Add char if frequency is 0
result.append(char)
# Increment frequency
ch[ord(char)] += 1
return ''.join(result)
# Driver code
s = "geeksforgeeks"
print(remove_duplicates(s))
C#
using System;
public class GfG {
// Function to remove duplicate characters
public static string RemoveDuplicatesChars(string s) {
// Create an integer array to store
// frequency for ASCII characters
int[] ch = new int[256];
System.Text.StringBuilder ans = new System.Text.StringBuilder();
// Traverse the input string
foreach (char c in s) {
// Check if current character's frequency is 0
if (ch[c] == 0) {
// Add char if frequency is 0
ans.Append(c);
// Increment frequency
ch[c]++;
}
}
return ans.ToString();
}
// Driver code
public static void Main() {
string s = "geeksforgeeks";
Console.WriteLine(RemoveDuplicatesChars(s));
}
}
JavaScript
function removeDuplicates(s) {
// Function to remove duplicate characters
// Create an integer array to store
// frequency for ASCII characters
let ch = new Array(256).fill(0);
let result = '';
// Traverse the input string
for (let char of s) {
// Check if current character's frequency is 0
if (ch[char.charCodeAt(0)] === 0) {
// Add char if frequency is 0
result += char;
// Increment frequency
ch[char.charCodeAt(0)]++;
}
}
return result;
}
// Driver code
let s = "geeksforgeeks";
console.log(removeDuplicates(s));
Time Complexity: O(n)
Auxiliary Space: O(1) considering that the alphabet size is constant.
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read