Monday 11 February 2013

VB.NET Find only Duplicate Values in an Array


VB.NET Find only Duplicate Values in an Array

Suppose array has series of numbers, if user wants to extract/find duplicated number in an array.
Here is the Logic.


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

    Dim query =

    For Each i As var In query.[Select](Function(ab, bc) ab).Where(Function(ab, bc) ab.Count() <> 1)
        Console.WriteLine(i.Key)
    Next



End Sub

Note :  1.Group each number by that number, so that duplicates will be under same group.
            2.if any group has more than one element          
                    query.Select((ab, bc) => ab.Count()) will be be greater than 1.
            3. Then filter the groups which has counter != 1  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:
10
20
100



i.e 10,20,100 repeated more than once . so these are duplicate values in an array.

Input:  int[] a = new int[] { 10, 20,  10,30, 30, 20, 40, 40, 12, 14,12,14 };
Output: 10
20
30
40
12
14
 i.e 10 ,20,30,40,12,14 numbers repeated more than one time in an array a.

Tags:
VB.NET  Find only Duplicate Values in an Array,get repeated elements in an array,get non-unique values from an array,IGrouping LINQ,LINQ Count,LINQ Select on IGrouping,C# Find only Duplicate Values in an Array using LINQ

No comments:

Post a Comment