String str2 = "ABABABCDEF";// ABCDEF String[] arr2 = str2.split(""); str2 = new LinkedHashSet<>(Arrays.asList(arr2)).toString().replace(", ", ""); System.out.println(str2); // ABCDEF