Advertisement
Guest User

Untitled

a guest
Mar 18th, 2022
27
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Main
  7. {
  8. static String removeDuplicateElements(char str[], int n)
  9. {
  10. // Used as index in the modified string
  11. int index = 0;
  12.  
  13. // Traverse through all characters
  14. for (int i = 0; i < n; i++)
  15. {
  16.  
  17. // Check if str[i] is present before it
  18. int j;
  19. for (j = 0; j < i; j++)
  20. {
  21. if (str[i] == str[j])
  22. {
  23. break;
  24. }
  25. }
  26.  
  27. // If not present, then add it to
  28. // result.
  29. if (j == i)
  30. {
  31. str[index++] = str[i];
  32. }
  33. }
  34. return String.valueOf(Arrays.copyOf(str, index));
  35. }
  36.  
  37. // Driver code
  38.  
  39. public static void main(String[] args) {
  40. Scanner scanner = new Scanner(System.in);
  41. String input = scanner.nextLine();
  42. char str[] = input.toCharArray();
  43. int n = str.length;
  44. System.out.println(removeDuplicateElements(str, n));
  45. }
  46. }
Advertisement
RAW Paste Data Copied
Advertisement