luoni

DistinctMethod

Jun 2nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4.  
  5. public class DistinctArray {
  6.  
  7. public static void main(String[] args) {
  8. String[] text = "one one one two three four five".split(" ");
  9. System.out.print(Arrays.toString(Distinct(text)));
  10. }
  11.  
  12. static String[] Distinct(String[] array) {
  13. String[] clone = array.clone();
  14.  
  15. for (int i = array.length - 1; i >= 0; i--) {
  16. for (int j = i; j > 0; j--) {
  17. if (clone[i].equals(clone[j - 1])) {
  18. clone[i] = "*";
  19. }
  20. }
  21. }
  22.  
  23. List<String> cloneAsList = new ArrayList<String>();
  24. for (String data : clone) {
  25. if (data != "*") {
  26. cloneAsList.add(data);
  27. }
  28. }
  29.  
  30. String[] result = cloneAsList.toArray(new String[cloneAsList.size()]);
  31. return result;
  32. }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment