Tuur Tuur - 2 months ago 10
C# Question

Disable a string in array to prevent duplicates

I have this application where i have to insert the same

string
in a
textbox
as the one in the
array
. The
strings
come by in a label, for every answer it will go to a random word/string that is in the array.

My question is: Is there any way to
delete
or
disable
a word/string in the
array
so I don't get any duplicates?

string[] arrProvincies = File.ReadAllLines(@"provincies.txt");

int counter = 0;
string Array = "";

string Array = arrProvincies[counter].ToString();
lblProvincie.Text = Array;
counter = rnd.Next(0, 12);

Answer

You can use a Queue:

Queue<string> arrProvincies = new Queue<string>(File.ReadAllLines(@"provincies.txt"));

And use it with lblProvincie.Text = arrProvincies.Dequeue();

Thus you'll be sure to have no duplicate since your remove the entry when you use it.


If you need shuffling:

Queue<string> arrProvincies = new Queue<string>(File.ReadAllLines(@"provincies.txt").OrderBy(o => Guid.NewGuid()));
Comments