Monday, 11 February 2013

VB.NET find maximum repeated number in an array

find maximum repeated number in an array VB.NET


Suppose array has series of numbers, if user wants to find maximum repeated number in an array.



Private Shared Sub FindMaximumRepeatedNumbers()
    Dim a As Integer() = New Integer() {10, 20, 100, 100, 100, 10, _
        30, 20, 40, 50, 12, 14}

    Dim query =


    Dim max As Integer = query.[Select](Function(ab, bc) ab.Count()).Max()

    Console.WriteLine(max)
    For Each i As var In query.[Select](Function(ab, bc) ab).Where(Function(ab, bc) ab.Count() = max)
        Console.WriteLine(i.Key)
    Next
End Sub


Note :  1.Group each number by that number, so that duplicates will be under same group.
            2.Get Which group has maximum elements             
                   int max = query.Select((ab, bc) => ab.Count()).Max();
            3. Then filter the groups which has max count got in step 2.
                 i.e this process may result more than one element.

Input : int[] a = new int[] { 10, 20, 100, 100, 100, 10, 30, 20, 40, 50, 12, 14 };
Output:  3-100 (i.e 100 repeated 3 times)
Input:  int[] a = new int[] { 10, 20,  10, 30, 20, 40, 50, 12, 14 };
Output: 2
10
20
 i.e 10 and 20 numbers repeated 2 times in an array.






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

No comments:

Post a Comment