Monday, 11 February 2013

VB.NET Find repeated numbers in an array

VB.NET  Find repeated numbers in an array

Integer Array has  numbers ,some are repeated, if you want to find which number is repeated how many time., Here is the logic using LINQ.
Private Shared Sub FindRepeatedNumbers()
    Dim a As Integer() = New Integer() {10, 20, 100, 100, 100, 10, _
        30, 20, 40, 50, 12, 14}

    Dim query =

    Dim maximum As Integer = a.Max()
    For Each intagroup As IGrouping(Of Integer, Integer) In query
        Console.WriteLine("Key={0},Repeated {1} Times", intagroup.Key, intagroup.Count())
End Sub

  1. First group by  each number
  2. there will a unique groups ,then find each group has how many numbers.
Here is the output.

Number=10,Repeated 2 Times
Number=20,Repeated 2 Times
Number=100,Repeated 3 Times
Number=30,Repeated 1 Times
Number=40,Repeated 1 Times
Number=50,Repeated 1 Times
Number=12,Repeated 1 Times
Number=14,Repeated 1 Times

Tags: VB.NET Find repeated numbers in an array,Find duplicate numbers in an array, find maximum repeated number in an array,find least repeated number in an array