score:0
Accepted answer
An approach
Dim list As New List(Of String) From {"a", "e", "e", "b", "c", "c", "b", "c", "d", "e"}
Dim duplicates As IEnumerable(Of String) = list.Distinct
duplicates = From s In duplicates
Where list.FindAll(Function(l) l = s).Count > 1
Select s
For Each s As String In duplicates
Dim ct As Integer = (From l In list Where l = s Select l).Count
Debug.WriteLine(s)
Debug.WriteLine("count:{0}", ct)
Dim idx As IEnumerable(Of Integer)
idx = From n In Enumerable.Range(0, list.Count)
Where list(n) = s
Select n Take ct
Debug.WriteLine("Indexes")
For Each n As Integer In idx
Debug.Write(n.ToString)
Debug.Write(" ")
Next
Debug.WriteLine("")
Next
Output
e
count:3
Indexes
1 2 9
b
count:2
Indexes
3 6
c
count:3
Indexes
4 5 7
score:2
You already have an answer, but I propose a slightly different method anyway, it might come in handy after. This uses the same LINQ methods you already have.
The LINQ's query creates an object that contains the value and position of all strings in the elements
List, groups the values then selects the groups that contain more than one item and finally orders the groups by their Key
value.
Dim elements = {"a", "b", "c", "c", "b", "c", "d", "e", "e"}.ToList()
Dim duplicates = elements.Select(Function(elm, i) New With {.Value = elm, .Index = i}).
GroupBy(Function(obj) obj.Value).
Where(Function(grp) grp.Count() > 1).
OrderBy(Function(grp) grp.Key)
For Each group In duplicates
For Each dupe In group
Console.WriteLine($"Duplicate: {dupe.Value} Index = {dupe.Index}")
Next
Console.WriteLine($"Number of {group.Key} duplicates: {group.Count()}")
Next
This prints:
Duplicate: b Index = 1
Duplicate: b Index = 4
Number of b duplicates: 2
Duplicate: c Index = 2
Duplicate: c Index = 3
Duplicate: c Index = 5
Number of c duplicates: 3
Duplicate: e Index = 7
Duplicate: e Index = 8
Number of e duplicates: 2
Source: stackoverflow.com
Related Articles
- Find, count and index the duplicate items in array
- Find index of a value in an array
- How to create MongoDB MultiKey index on attribute of items in an array .NET Driver
- Find multiple index in array
- How to find duplicate items in list<>?
- How to find duplicate items based on multiple values using LINQ?
- count items in array except value that equal to -1
- Count of duplicate items in a C# list
- Fastest way to find duplicate items in a list in C#
- c# - LINQ find 2D jagged array minimum, return index
- Find items with duplicate values over mutiple properties
- Find and count from array in another array using linq C#
- Find index of item from string array containing comma separated values
- How to add Count if the duplicate index is occur when add list to another list?
- How to find the index of an element in an array inside a lambda expression in c#
- Find duplicate items in list based on particular member value
- Find the index position of duplicate entries in a comma separated string
- Linq code to get the index of an object in an array from an object within a list
- Find matching tag items in table from string array using linq
- Find index from an array using regex with linq in c# .net
- Linq Dictionary for count of items that are in an array
- C# Count Items across an Array of Lists
- Find index of array with specific length in a List?
- List or Array of String Contain specific word in Html Source Code
- Removing items in Jagged array using index
- how to simplify code to print all items in an array use one line code in c#
- Linq Find Partial Text Match - Included code returns duplicate and everything except what it should
- Filtering Duplicate items in an array based on child array in c#
- How to find index of a value in array of char?
- Using LINQ to find duplicate min values in array C#
- Passing a List of Strings to a where clause in a LINQ Query
- How to dynamically populate XElement (linq to xml)
- Escape special characters in LINQ contains method
- Left Join In Entity Framework 6
- LINQ subquery with AND operator displays no results
- C# -LINQ- Extension Method
- Truncate Time Converting list to nullable
- GroupBy and Count using LINQ
- linq many to many query inside select
- LINQ First vs Find performance considerations
- How does Linq projection work between Extention Method and query expression
- Use Any() and Count() in Dynamic Linq
- How to convert int array to List<KeyValuePair<int, string>>?
- Re-implementation of OrderBy, ThenBy and Null TypeMapping in Sql Tree error
- how to perform a bulk update with a list of objects linq c#
- EntityFramework/LINQ: filter a hierarchical list
- Using a Substring in LINQ causes query to time out?
- In-memory LINQ performance
- Selecting all child objects in Linq
- Unable to return list in linq query when using different tables with inner joins