Avoid duplication without sets?

I am in the Java class for beginners, and I have not had the opportunity to learn how to avoid duplicate values โ€‹โ€‹when storing values โ€‹โ€‹inside arrays.

String[] newAlphabet = new String[26];

for(int I = 0; I < newAlphabet.length; I++){

 int random = (65 + (int)(Math.random() * ((90 - 65) + 1));
 char ascii = (char)random;
 String letters = ascii + "";

if(letters != newAlphabet[0] && letters != newAlphabet[1] ... so on and so on until
                                                               newAlphabet[25])
     newAlphabet[I] = letters; 
}//end 

So, this is my pseudo code for part of my program, and the gist of it is to avoid duplicating letters inside the array.

The problem I encountered is inside the if statement. Instead of entering letters != newAlphabet[]up to 25, is there any other way to do this?

I saw some of the forums on stackedoverflow that I should use HashSet, but I didn't find out? I can ask my teacher if I allow, but is there any other way to avoid this problem?

I thought about using a search for all elements of the array for each cycle, but I did not think over the plan long enough if it is valid.

+4
5

Java, , . , , , , , , , , .

-, . , , , , . ...

boolean exists = false; //indicates whether we have found a match
for (int j = 0; j < 26; j++) { //for each letter in the new alphabet
    //true if this one, or a previous one is a match
    exists = exists || letters == newAlphabet[i]; 
}
//if we don't have a match, add the new letter
if (!exists) newAlphabet[I] = letters;

, , 26 , , :

boolean exists = false; 
for (int j = 0; j < I; j++) { //note in this line we stop before the insertion point
    exists = exists || letters == newAlphabet[i]; 
}
if (!exists) newAlphabet[I] = letters;

, , , , , :

boolean exists = false;
int j = 0;
while (!exists && j < I) { //we now also stop if we have already found a match
    exists = letters == newAlphabet[i]; 
    //as we are stopping at the first match, 
    //we no longer need to allow for previous matches
}
if (!exists) newAlphabet[I] = letters;
+3

asList:

if( Arrays.asList(newAlphabet).contains(letters) ) {
    newAlphabet[I] = letters; 
}

, 26 , .

: asList - Arrays. , Arrays . Arrays.asList() . asList (newAlhpabet) java.util.List. , List . contains() - List, true, List , (letters).

+3

, , , A to Z :

int random = (65 + (int)(Math.random() * ((90 - 65) + 1));

, , , :

// Initialize new alphabet array
String originalAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] newAlphabet = originalAlphabet.toCharArray();
// Shuffle the new alphabet by swapping each character to a random position
for (int i=0; i<26; i++) {
  int j = (int)(Math.random() * 26);
  char temp = newAlphabet[i];
  newAlphabet[i] = newAlphabet[j];
  newAlphabet[j] = temp;
}
// Print the new alphabet
for (int i=0; i<26; i++) {
  System.out.print(newAlphabet[i]);
}
System.out.println();

: VYMTBIPWHKZNGUCDLRAQFSOEJX

+2
0

:

if(Arrays.binarySearch(newAlphabet, letters) < 0){
  newAlphabet[I] = letters;
 }

while, , , Arrays.binarySearch, (-(insertion index) - 1), , .

-3

Source: https://habr.com/ru/post/1537902/


All Articles