What is wrong in the code? Why does not it work? It is necessary to remove duplicates from the array.

public class Dublicate { public String[] delDublicate(String[] args) { StringBuffer result = new StringBuffer(); StringBuffer esult = new StringBuffer(); int i=1; for (int j=0; j <args.length; j++) { result.append(args[i]); String res = result.toString(); esult.append(args[j]); String re = esult.toString(); if (res.equals(re)){ if (args[j]!= args[i]) { args[i] = " "; } } } return args; } public static void main(String[] args){ String [] res={"Привет", "Привет", "задание", "выполнено", "верно", "задание"}; Dublicate dub=new Dublicate(); dub.delDublicate(res); System.out.print(Arrays.deepToString(res)); } } 
  • 5th line int i = 1; what she does? You use it as a key to access the elements of the array, but you have it all the time 1. - XelaNimed
  • This is an array index, for clarity, even if instead of drawing a for loop with an increase of 1, the code does not give the expected result, I try to understand where the error is - Azamat
  • You compare res.equals (re), while res is filled with "Hi" at each iteration, you will never go inside if. In general, why use StringBuffer? - a.chugunov
  • With args [0], I also don’t get into it, although I have to, StringBuffer to format from args [i] to String - Azamat

3 answers 3

 import java.util.Arrays; public class Dublicate { public void delDublicate(String[] args) { for (int i = args.length-1; i > 0; i--) { StringBuffer result = new StringBuffer(); result.append(args[i]); String res = result.toString(); for (int j = 0; j < args.length; j++) { StringBuffer esult = new StringBuffer(); esult.append(args[j]); String re = esult.toString(); if (i != j) { if (res.equals(re)) { args[i] = ""; } } } } } public static void main(String[] args) { String[] res = {"Привет", "Привет", "задание", "выполнено", "верно", "задание"}; Dublicate dub = new Dublicate(); dub.delDublicate(res); System.out.print(Arrays.deepToString(res)); } } 
  • Removed spaces, did not help - Azamat
  • how do you propose to write NOT EQUALLY using equals? I need to compare indexes and not values, values ​​I compare here: res.equals (re), i.e. the same values ​​in different indices - Azamat
  • one
    ! str1.equals (str2) here's your not equal. And specify which indices you compare here: args [j]! = Args [i]? Look here for a comparison of the lines. Stackoverflow.com/questions/417405/… - Vladislav Kuznetsov
  • one
    Between the cycles, the useless initialization code of the StringBuffer object and dropping it back to the string - a.chugunov
  • one
    Thank you very much! - Azamat

I would do something like this

 public String[]delDublicate(String[] args) { List<String> withoutDublicate = new ArrayList<>(); for (String str : args) { if (!withoutDublicate.contains(str)) { withoutDublicate.add(str); } } String[] returnArr = new String[withoutDublicate.size()]; return withoutDublicate.toArray(returnArr); } public static void main(String[] args){ String [] res={" Привет ", " Привет", " задание", " выполнено", " верно", " задание"}; Dublicate dub=new Dublicate(); String[] arr = dub.delDublicate(res); System.out.print(Arrays.deepToString(arr)); } 

    You can try this:

      String[] res = {"Привет", "Привет", "задание", "выполнено", "верно", "задание"}; String[] array = Arrays.stream(res).distinct().toArray(String[]::new); System.out.println(Arrays.toString(array));