Greetings The topic may be a bit jagged, but I never found a solution. There is an array of strings, how to delete (just remove duplicates, and not just find them). thank

  • There are two options: move the tail or create a copy of the array, skipping duplicates. So what is next? Should the row order be maintained? - alexlz
  • Do you need the elements in the array to remain in only one instance? - Kerins_Sataier
  • 3
    Well, the fact that a new array is optional. Options: put the lines in the set (for example HashSet), then choose. (In the set of identical elements one by one). Option 2: sort the array of strings, then copy the strings that do not match the previous one. But this question used to be exactly. - alexlz
  • 3
    adding each element in HashSet we automatically get rid of duplicates. Well, then we turn the resulting HashSet into an array. - Viacheslav
  • one
    @ baralgin1003, do not forget to accept the correct answers! - angry

4 answers 4

String[] str = {"aa", "bb", "cc", "aa"}; Set<String> set = new HashSet<String>(Arrays.asList(str)); String[] result = set.toArray(new String[set.size()]); 
  • one
    I would like to be shorter - alexlz
  • 3
    Much shorter ... You can drive two lines into one line, in principle: String [] result = new HashSet <String> (Arrays.asList (str)). ToArray (new String [0]); - Nofate
  • 3
    Yes. Here you will not put a smiley, and it begins ... - alexlz
  • one
    I apologize, it's a hard day. - Nofate
 void removeDuplicates(String[] array) { HashSet<String> hash = new HashSet<String>(); for (int i = 0 ; i < array.length; i++) { array[i] = hash.add(array[i]) ? array[i] : null; } } 
  • one
    Sorry, I am bad in Java, but I have a doubt. From [1, 2, 1, 1, 3] will it not make [1, 2, null, null, 3] instead of [1, 2, 3] ? - drdaeman
  • Must. Removing nulls stays on your share - alexlz
  • If it is required to get [1, 2, 3] from [1, 2, 1, 1, 3], then this is not a deletion from a specific array (object) but the creation of a new one without repetitions (the size of the array cannot change). If the question is exactly what was meant, then my decision really does not fit. - Sinitsyn Artem
  • Indeed, it is not clear from the question what exactly it means to delete. Maybe as you did, maybe like @Nofate, or maybe everything that does not repeat to the beginning is shifted. - avp
  • 2
    @ baralgin1003, why answer your question? It is necessary to note the answer for the correct one - on the left there is a tick in the circle .. - alex_90
 window.deleteDublicates = function(arr) { var uniqs = {} for (var i=0; i<arr.length; i++){ uniqs[arr[i]] ? true : uniqs[arr[i]] = {} } return Object.keys(uniqs) } 
     String[] sA = {"слова", "которые", "не", "просто", "слова", "а", "массив", "слов"}; String[] sB = new String[sA.length]; HashMap <String, String> shMap = new HashMap<String, String>(); for (int i = 0; i < sA.length; i++) shMap.put(sA[i], ""); int j=0; for (int i = 0; i < sA.length; i++) { if( shMap.get(sA[i]).equals("")) { shMap.put(sA[i], sA[i]); sB[j++] = sA[i]; } } System.out.println(sA[i] + " "); System.out.println(sB[i] + " ");