xt4k

Untitled

Dec 10th, 2021
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4.  
  5. public class UniqueChar {
  6. public static void main(String[] args) {
  7. String inputString1 = "dfeddsaagrtrf";
  8. // String inputString1 = "GeeksForGeeks";
  9. // String inputString1 = "GeeksAForGeeks";
  10.  
  11. String sym = searchForSymbol(inputString1);
  12. System.out.println("returned: " + sym);
  13. }
  14.  
  15. private static String searchForSymbol(String inputString1) {
  16. List<String> unique = new ArrayList<>();
  17. String[] chars = inputString1.split("");
  18.  
  19. List<String> chrs = Arrays.asList(chars);
  20. System.out.println("chrs: " + chrs);
  21. int size = chrs.size()-1;
  22.  
  23. for (int i = 0; i <= size; i++) {
  24. String symbol = chrs.get(i);
  25. System.out.println("search for symbol: " + symbol);
  26. boolean isDuplicated = false;
  27.  
  28. for (int j = 0; j <= size; j++) {
  29. if (j!=i) {
  30. if (symbol.equals(chrs.get(j))) {
  31. System.out.println("paired symbol " + symbol);
  32. isDuplicated = true;
  33. }
  34. }
  35. }
  36.  
  37. if (!isDuplicated) {
  38. if (!unique.contains(symbol)) {
  39. System.out.println("new un-paired symbol " + symbol);
  40. unique.add(symbol);
  41. }
  42. }
  43. }
  44. System.out.println("unique list" + unique);
  45. return unique.get(0);
  46. }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment