Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Java/RemoveDuplicatesFromString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.*;

class RemoveDuplicatesFromString
{
static String removeDuplicate(char str[], int n)
{
// Used as index in the modified string
int index = 0;

// Traverse through all characters
for (int i = 0; i < n; i++)
{

// Check if str[i] is present before it
int j;
for (j = 0; j < i; j++)
{
if (str[i] == str[j])
{
break;
}
}

// If not present, then add it to
// result.
if (j == i)
{
str[index++] = str[i];
}
}
return String.valueOf(Arrays.copyOf(str, index));
}

// Driver code
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
char str[] = s.toCharArray();
int n = str.length;
System.out.println(removeDuplicate(str, n));
}
}