Remove duplicates from a given string | C ++ program to create a unique string ; Function to make the string unique ; loop to traverse the string and check for repeating chars using IndexOf ( ) method in Java ; character at i 'th index of s ; If c is present in str , it returns the index of c , else it returns npos ; Adding c to str if npos is returned ; Driver code ; Input string with repeating chars
#include <bits/stdc++.h> NEW_LINE using namespace std ; string unique ( string s ) { string str ; int len = s . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { char c = s [ i ] ; auto found = str . find ( c ) ; if ( found == std :: string :: npos ) { str += c ; } } return str ; } int main ( ) { string s = " geeksforgeeks " ; cout << unique ( s ) << endl ; }