1

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.

2
  • Using the LINQ extension methods: Dim uniques = numbers.Distinct().ToArray(). Commented Apr 8, 2014 at 22:11
  • 1
    @Styxxy: The Distinct method doesn't remove values that have duplicates, it only removes the duplicates and leaves one of each value. Commented Apr 8, 2014 at 22:52

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

I'm very sorry but that is WAY above novice level...I don't understand any of it...
0

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()))

2 Comments

OK, I think I follow this...but how do I get the unique values into a new listbox?
Use LINQ to get unique values : Dim unique = numbers.Except(duplicates).Distinct(). What's next? How to display unique in ListBox?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.