Print all distinct characters of a string in order (3 Methods)
Last Updated :
25 Jun, 2022
Given a string, find the all distinct (or non-repeating characters) in it. For example, if the input string is “Geeks for Geeks”, then output should be ‘for’ and if input string is “Geeks Quiz”, then output should be ‘GksQuiz’.
The distinct characters should be printed in same order as they appear in input string.
Examples:
Input : Geeks for Geeks
Output : for
Input : Hello Geeks
Output : HoGks
Method 1 (Simple : O(n2))
A Simple Solution is to run two loops. Start traversing from left side. For every character, check if it repeats or not. If the character doesn’t repeat, increment count of non-repeating characters. When the count becomes 1, return each character.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str = "GeeksforGeeks" ;
for ( int i = 0; i < str.size(); i++)
{
int flag = 0;
for ( int j = 0; j < str.size(); j++)
{
if (str[i] == str[j] and i != j)
{
flag = 1;
break ;
}
}
if (flag == 0)
cout << str[i];
}
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void main(String[] args)
{
String str = "GeeksforGeeks" ;
for ( int i = 0 ; i < str.length(); i++)
{
int flag = 0 ;
for ( int j = 0 ; j < str.length(); j++)
{
if (str.charAt(i) == str.charAt(j) && i != j)
{
flag = 1 ;
break ;
}
}
if (flag == 0 )
System.out.print(str.charAt(i));
}
}
}
|
Python3
string = "GeeksforGeeks"
for i in range ( 0 , len (string)):
flag = 0
for j in range ( 0 , len (string)):
if (string[i] = = string[j] and i! = j):
flag = 1
break
if (flag = = 0 ):
print (string[i],end = "")
|
C#
using System;
public class GFG{
public static void Main(String[] args)
{
String str = "GeeksforGeeks" ;
for ( int i = 0; i < str.Length; i++)
{
int flag = 0;
for ( int j = 0; j < str.Length; j++)
{
if (str[i] == str[j] && i != j)
{
flag = 1;
break ;
}
}
if (flag == 0)
Console.Write(str[i]);
}
}
}
|
Javascript
<script>
var str = "GeeksforGeeks" ;
for ( var i = 0; i < str.length; i++) {
var flag = 0;
for (j = 0; j < str.length; j++) {
if (str.charAt(i) == str.charAt(j) && i != j) {
flag = 1;
break ;
}
}
if (flag == 0)
document.write(str.charAt(i));
}
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Method 2 (Efficient but requires two traversals: O(n))
- Create an array count[] to store counts of characters.
- Traverse the input string str and do following for every character x = str[i].
Increment count[x].
- Traverse the input string again and do following for every character str[i]
- If count[x] is 1, then print the unique character
- If count[x] is greater than 1, then ignore the repeated character.
Below is the implementation of above idea.
C++
# include <iostream>
using namespace std;
# define NO_OF_CHARS 256
void printDistinct( char *str)
{
int count[NO_OF_CHARS];
int i;
for (i = 0; *(str+i); i++)
if (*(str+i)!= ' ' )
count[*(str+i)]++;
int n = i;
for (i = 0; i < n; i++)
if (count[*(str+i)] == 1)
cout<< str[i];
}
int main()
{
char str[] = "GeeksforGeeks" ;
printDistinct(str);
return 0;
}
|
Java
public class GFG {
static final int NO_OF_CHARS = 256 ;
static void printDistinct(String str)
{
int [] count = new int [NO_OF_CHARS];
int i;
for (i = 0 ; i < str.length(); i++)
if (str.charAt(i)!= ' ' )
count[( int )str.charAt(i)]++;
int n = i;
for (i = 0 ; i < n; i++)
if (count[( int )str.charAt(i)] == 1 )
System.out.print(str.charAt(i));
}
public static void main(String args[])
{
String str = "GeeksforGeeks" ;
printDistinct(str);
}
}
|
Python3
NO_OF_CHARS = 256
def printDistinct( str ):
count = [ 0 ] * NO_OF_CHARS
for i in range ( len ( str )):
if ( str [i] ! = ' ' ):
count[ ord ( str [i])] + = 1
n = i
for i in range (n):
if (count[ ord ( str [i])] = = 1 ):
print ( str [i], end = "")
if __name__ = = "__main__" :
str = "GeeksforGeeks"
printDistinct( str )
|
C#
using System;
public class GFG {
static int NO_OF_CHARS = 256;
static void printDistinct(String str)
{
int [] count = new int [NO_OF_CHARS];
int i;
for (i = 0; i < str.Length; i++)
if (str[i]!= ' ' )
count[( int )str[i]]++;
int n = i;
for (i = 0; i < n; i++)
if (count[( int )str[i]] == 1)
Console.Write(str[i]);
}
public static void Main()
{
String str = "GeeksforGeeks" ;
printDistinct(str);
}
}
|
Javascript
<script>
let NO_OF_CHARS = 256;
function printDistinct(str)
{
let count = new Array(NO_OF_CHARS);
for (let i=0;i<NO_OF_CHARS;i++)
{
count[i]=0;
}
let i;
for (i = 0; i < str.length; i++)
if (str[i]!= ' ' )
count[str[i].charCodeAt(0)]++;
let n = i;
for (i = 0; i < n; i++)
if (count[str[i].charCodeAt(0)] == 1)
document.write(str[i]);
}
let str = "GeeksforGeeks" ;
printDistinct(str);
</script>
|
Output:
for
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3 (O(n) and requires one traversal)
The idea is to use two auxiliary arrays of size 256 (Assuming that characters are stored using 8 bits).
- Initialize all values in count[] as 0 and all values in index[] as n where n is length of string.
- Traverse the input string str and do following for every character c = str[i].
- Increment count[x].
- If count[x] is 1, then store index of x in index[x], i.e., index[x] = i
- If count[x] is 2, then remove x from index[], i.e., index[x] = n
- Now index[] has indexes of all distinct characters. Sort indexes and print characters using it. Note that this step takes O(1) time assuming number of characters are fixed (typically 256)
Below is the implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 256;
void printDistinct(string str)
{
int n = str.length();
int count[MAX_CHAR];
int index[MAX_CHAR];
for ( int i = 0; i < MAX_CHAR; i++)
{
count[i] = 0;
index[i] = n;
}
for ( int i = 0; i < n; i++)
{
char x = str[i];
++count[x];
if (count[x] == 1 && x != ' ' )
index[x] = i;
if (count[x] == 2)
index[x] = n;
}
sort(index, index+MAX_CHAR);
for ( int i=0; i<MAX_CHAR && index[i] != n; i++)
cout << str[index[i]];
}
int main()
{
string str = "GeeksforGeeks" ;
printDistinct(str);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static final int MAX_CHAR = 256 ;
static void printDistinct(String str)
{
int n = str.length();
int [] count = new int [MAX_CHAR];
int [] index = new int [MAX_CHAR];
for ( int i = 0 ; i < MAX_CHAR; i++)
{
count[i] = 0 ;
index[i] = n;
}
for ( int i = 0 ; i < n; i++)
{
char x = str.charAt(i);
++count[x];
if (count[x] == 1 && x != ' ' )
index[x] = i;
if (count[x] == 2 )
index[x] = n;
}
Arrays.sort(index);
for ( int i = 0 ; i < MAX_CHAR && index[i] != n;
i++)
System.out.print(str.charAt(index[i]));
}
public static void main(String args[])
{
String str = "GeeksforGeeks" ;
printDistinct(str);
}
}
|
Python
MAX_CHAR = 256
def printDistinct( Str ):
n = len ( Str )
count = [ 0 for i in range (MAX_CHAR)]
index = [n for i in range (MAX_CHAR)]
for i in range (n):
x = ord ( Str [i])
count[x] + = 1
if (count[x] = = 1 and x ! = ' ' ):
index[x] = i
if (count[x] = = 2 ):
index[x] = n
index = sorted (index)
for i in range (MAX_CHAR):
if index[i] = = n:
break
print ( Str [index[i]],end = "")
Str = "GeeksforGeeks"
printDistinct( Str )
|
C#
using System;
public class GFG {
static int MAX_CHAR = 256;
static void printDistinct( string str)
{
int n = str.Length;
int []count = new int [MAX_CHAR];
int []index = new int [MAX_CHAR];
for ( int i = 0; i < MAX_CHAR; i++)
{
count[i] = 0;
index[i] = n;
}
for ( int i = 0; i < n; i++)
{
char x = str[i];
++count[x];
if (count[x] == 1 && x != ' ' )
index[x] = i;
if (count[x] == 2)
index[x] = n;
}
Array.Sort(index);
for ( int i = 0; i < MAX_CHAR &&
index[i] != n; i++)
Console.Write(str[index[i]]);
}
public static void Main()
{
string str = "GeeksforGeeks" ;
printDistinct(str);
}
}
|
Javascript
<script>
let MAX_CHAR = 256;
function printDistinct(str)
{
let n = str.length;
let count = new Array(MAX_CHAR);
let index = new Array(MAX_CHAR);
for (let i = 0; i < MAX_CHAR; i++)
{
count[i] = 0;
index[i] = n;
}
for (let i = 0; i < n; i++)
{
let x = str[i].charCodeAt(0);
++count[x];
if (count[x] == 1 && x != ' ' )
index[x] = i;
if (count[x] == 2)
index[x] = n;
}
index.sort( function (a,b){ return a-b});
for (let i = 0; i < MAX_CHAR && index[i] != n;
i++)
document.write(str[index[i]]);
}
let str = "GeeksforGeeks" ;
printDistinct(str);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...