0

I am creating a simple name picker from a specified list. How should I modify it so that it doesn't duplicate already "picked" names?

static void Main(string[] args)
{

    string[] Names = { "Erik", "Levente", "Noel", "Áron", "Krisztián", "Kristóf", "Bence", "Roland", "Máté", "László", "Bálint" ,
    "Regina", "Brigitta", "Gréta", "Hédi", "Hanna", "Boglárka", "Jázmin", "Réka", "Alexandra", "Rebeka", "Lili", "Luca", "Zsófi"};

    Random rnd2 = new Random();
    Console.WriteLine("Az ID : '1' eszköz nyertese: " + Names[rnd2.Next(0, Names.Length - 1)]);

    Random rnd3 = new Random();
    Console.WriteLine("Az ID : '2' eszköz nyertese: " + Names[rnd2.Next(0, Names.Length - 1)]);

    Random rnd4 = new Random();
    Console.WriteLine("Az ID : '3' eszköz nyertese: " + Names[rnd2.Next(0, Names.Length - 1)]);

    Random rnd5 = new Random();
    Console.WriteLine("Az ID : '4' eszköz nyertese: " + Names[rnd2.Next(0, Names.Length - 1)]);

    Random rnd6 = new Random();
    Console.WriteLine("Az ID : '5' eszköz nyertese: " + Names[rnd2.Next(0, Names.Length - 1)]);

    Random rnd7 = new Random();
    Console.WriteLine("Az ID : '6' eszköz nyertese: " + Names[rnd2.Next(0, Names.Length - 1)]);


    Console.ReadKey(true);
}
Dillon Davis
  • 6,679
  • 2
  • 15
  • 37

1 Answers1

0

You can ensure you don't have duplicates by storing what you've already used in a List<string> for example:

string[] names = new string[] { "bob", "james", "tim", "amanda", "lisa" }
List<string> alreadyUsed = new List<string>();
Random r = new Random();
while (alreadyUsed.Count < names.Length) {
    int index = r.Next(0, names.Length);
    if (!alreadyUsed.Contains(names[index])) {
        alreadyUsed.Add(names[index]);
        Console.WriteLine($"{names[index]} was added.");
    }
}
Hazel へいぜる
  • 2,751
  • 1
  • 12
  • 44
  • 2
    Why List, when HashSet is more suitable? – Vaptsarov Jul 31 '18 at 07:36
  • `List` is more common IMO; I have actually never used `HashSet` in 12 years of development. I'm sure it has its uses, so I will look into it for sure. – Hazel へいぜる Jul 31 '18 at 07:37
  • 4
    @davisj1691: well, it has it's uses, for example here. Always when you need to store unique values and when you want fast lookup even if the list is very large. It just doesn't allow random access, it isn't ordered. So whenever you feel you need a dictionary with keys only, then `HashSet` is your class and when you need to access items via index or you need an ordered collection that allows duplicates, then use a `List`(or array). – Tim Schmelter Jul 31 '18 at 07:38
  • @TimSchmelter very good information, this may actually help with a performance issue a coworker has been having. I recommended a `Parallel.ForEach` which indeed helped by reducing processing time by 35% (about 3 minutes). This may also help with his rather large data sets. – Hazel へいぜる Jul 31 '18 at 07:49
  • The point that you brought up about performance this would be a good idea for the nested loop, but I think for the OP's question a single loop should suffice? – Hazel へいぜる Jul 31 '18 at 07:57
  • And in this situation how can i loop this and display the result with 'x' times? – Mihály Kolmankó Jul 31 '18 at 08:10
  • Please make sure you mark the answer if you feel your issue has been resolved. – Hazel へいぜる Jul 31 '18 at 08:13
  • davisj1691, Hey, can u please edit the code for looping X times and display the result? Like The 1st winner is: 'xy' – Mihály Kolmankó Jul 31 '18 at 08:16
  • @MihályKolmankó please feel free to create a new question to have the new issue resolved. – Hazel へいぜる Jul 31 '18 at 08:23
  • @davisj1691 I cant post a new one, only one every 90 mins . :( – Mihály Kolmankó Jul 31 '18 at 08:27