I have an array which has some duplicate values.
I have to count the number of each duplicate items and their index.
Print such as:
Index of b: 1
Index of b: 4
Index of c: 2
Index of c: 3
Index of c: 5
Index of e: 7
Index of e: 8
Total duplicate b: 2
Total duplicate c: 3
Total duplicate e: 2
In my code I'm able to find only duplicate values.
Dim list As New List(Of String) From {"a", "b", "c", "c", "b", "c", "d", "e", "e"}
Dim duplicates = list.GroupBy(Function(x) x).Where(Function(x) x.Count > 1).Select(Function(x) x.Key)
For Each i As String In list.ToArray
If duplicates.Contains(i) Then
Debug.WriteLine("Duplicate Element: " & i & "Index: " & list.IndexOf(i))
End If
Next
Which gives output:
Duplicate Element: bIndex: 1
Duplicate Element: cIndex: 2
Duplicate Element: cIndex: 2
Duplicate Element: bIndex: 1
Duplicate Element: cIndex: 2
Duplicate Element: eIndex: 7
Duplicate Element: eIndex: 7