Given a string S, the task is to remove all the duplicates in the given string.
Below are the different methods to remove duplicates in a string.
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>
|
Output:
geksfor
Time Complexity : O(n * n)
Auxiliary Space : O(1)
Keeps order of elements the same as input.
METHOD 2 (Use BST)
use set which implements a self-balancing Binary Search Tree.
Javascript
<script>
function removeDuplicate( str , n)
{
var s = new Set();
for ( var i = 0;i<n;i++)
s.add(str[i]);
for (const v of s) {
document.write(v);
}
}
var str = "geeksforgeeks" ;
var n = str.length;
removeDuplicate(str, n);
</script>
|
Output:
efgkors
Time Complexity: O(n Log n)
Auxiliary Space: O(n)
Thanks to Anivesh Tiwari for suggesting this approach.
It does not keep the order of elements the same as the input but prints them in sorted order.
METHOD 3 (Use Sorting)
Algorithm:
1) Sort the elements.
2) Now in a loop, remove duplicates by comparing the
current character with previous character.
3) Remove extra characters at the end of the resultant string.
Example:
Input string: geeksforgeeks
1) Sort the characters
eeeefggkkorss
2) Remove duplicates
efgkorskkorss
3) Remove extra characters
efgkors
Note that, this method doesn’t keep the original order of the input string. For example, if we are to remove duplicates for geeksforgeeks and keep the order of characters the same, then the output should be geksfor, but the above function returns efgkos. We can modify this method by storing the original order.
Implementation:
Javascript
<script>
function removeDuplicate(string)
{
return string.split( '' )
.filter( function (item, pos, self)
{
return self.indexOf(item) == pos;
}
).join( '' );
}
var str = "geeksforgeeks" ;
document.write( " " +removeDuplicate(str));
</script>
|
Output:
efgkors
Time Complexity: O(n log n) If we use some nlogn sorting algorithm instead of quicksort.
Auxiliary Space: O(1)
METHOD 4 (Use Hashing )
Algorithm:
1: Initialize:
str = "test string" /* input string */
ip_ind = 0 /* index to keep track of location of next
character in input string */
res_ind = 0 /* index to keep track of location of
next character in the resultant string */
bin_hash[0..255] = {0,0, ….} /* Binary hash to see if character is
already processed or not */
2: Do following for each character *(str + ip_ind) in input string:
(a) if bin_hash is not set for *(str + ip_ind) then
// if program sees the character *(str + ip_ind) first time
(i) Set bin_hash for *(str + ip_ind)
(ii) Move *(str + ip_ind) to the resultant string.
This is done in-place.
(iii) res_ind++
(b) ip_ind++
/* String obtained after this step is "te stringing" */
3: Remove extra characters at the end of the resultant string.
/* String obtained after this step is "te string" */
Implementation:
Javascript
<script>
function removeDuplicates( str) {
var lhs = new Set();
for ( var i = 0; i < str.length; i++)
lhs.add(str[i]);
for ( var ch of lhs)
document.write(ch);
}
var str = "geeksforgeeks" ;
removeDuplicates(str);
</script>
|
Output:
geksfor
Time Complexity: O(n)
Important Points:
- Method 2 doesn’t maintain the characters as original strings, but method 4 does.
- It is assumed that the number of possible characters in the input string is 256. NO_OF_CHARS should be changed accordingly.
- calloc() is used instead of malloc() for memory allocations of a counting array (count) to initialize allocated memory to ‘�’. the malloc() followed by memset() could also be used.
- The above algorithm also works for integer array inputs if the range of the integers in the array is given. An example problem is to find the maximum occurring number in an input array given that the input array contains integers only between 1000 to 1100
Method 5 (Using IndexOf() method) :
Prerequisite : Java IndexOf() method
Javascript
<script>
function unique(s)
{
let str = "" ;
let len = s.length;
for (let i = 0; i < len; i++)
{
let c = s[i];
if (str.indexOf(c) < 0)
{
str += c;
}
}
return str;
}
let s = "geeksforgeeks" ;
document.write(unique(s));
</script>
|
Output:
geksfor
Thanks debjitdbb for suggesting this approach.
Method 6 (Using unordered_map STL method) :
Prerequisite : unordered_map STL C++ method
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>
|
Output:
geksfor
Time Complexity : O(n)
Auxiliary Space : O(n)
Thanks, Allen James Vinoy for suggesting this approach.
Please refer complete article on Remove duplicates from a given string for more details!