Question: I need to generate a list of random (integer) elements for an array, then display unique values. Getting the random elements is easy:

```
Dim ro As New Random
Dim numbers(19) As Integer
Dim counter As Integer

```

Private Sub btnAdd\_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

```
  numbers(counter) = ro.Next(10, 101)
  lstNumEntered.Items.Add(numbers(counter))
  counter += 1

```

OK, now I have to display in another listbox any/all values that are NOT duplicates, so first I have to identify the duplicates. That's where I'm stuck..... This solution has to be at the novice level or you will lose me.

Answer: 
You can group the array on the values, and the groups containing more than one item are the duplicates:

```
Dim duplicates As List(Of Integer) = _
  numbers.GroupBy(Function(n) n) _
  .Where(Function(g) g.Count() > 1) _
  .Select(Function(g) g.First) _
  .ToList()

```
I think LINQ, as pointed by @Guffa, is the most convenient way to accomplish this task. But you can also accomplish this with basic `For` loop operations. 

Have a temporary collection to store distinct values (`distinct` in below example), then in every `For` loop iteration, if you encounter number that already exist in that collection, you know exactly that the number is a duplicate :

```
'variable to store distinct numbers (either duplicated or not)
Dim distinct As New List(Of Integer)
'variable to store duplicated numbers
Dim duplicates As New List(Of Integer)

For i As Integer = 0 To numbers.Length - 1
    'if distinct doesn't contain number(i), means this number isn't duplicated SO FAR
    If Not distinct.Contains(numbers(i)) Then
        distinct.Add(numbers(i))
    'else, means distinct already contains the number, means this numbers(i) is a DUPLICATE
    'if the number hasn't been added to duplicates, add it
    ElseIf Not duplicates.Contains(numbers(i)) Then
        duplicates.Add(numbers(i))
    End If
Next

'following lines are for debugging purpose only
'print all numbers
Console.WriteLine(String.Join(",", numbers.Select(Function(x) x.ToString()).ToArray()))
'print all duplicates
Console.WriteLine(String.Join(",", duplicates.Select(Function(x) x.ToString()).ToArray()))
'print distinct numbers
Console.WriteLine(String.Join(",", distinct.Select(Function(x) x.ToString()).ToArray()))

```