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;
}
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.