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 Query
- Find, count and index the duplicate items in array
- Convert an array to dictionary with value as index of the item and key as the item itself
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- Find and count from array in another array using linq C#
- How to add Count if the duplicate index is occur when add list to another list?
- Linq into the array then find values in the array and list to combobox
- How to find the index of an element in an array inside a lambda expression in c#
- How to find keys in a Dict<int,List<Tuple<string,string>>> such that the list contains elements with given Item1 and Items
- Find the index position of duplicate entries in a comma separated string
- Remove duplicate items from list if certain properties match and get the top item based on some ordering
- merging 2 collections and find all the unique items
- Linq code to get the index of an object in an array from an object within a list
- What's the fastest way to find and delete duplicate nodes inside XML?
- How to find duplicate dates in a list and group the description
- Find items with same property from list and pick the cheaper of the two items
- Parallel.Invoke and Index was outside the bounds of the array
- Querying a list to find the count of an ID and merge the contents of two similar IDs
- How to count write the linq query when the grouped bY column is in one table and the items are in another table
- Best way to find if a value is present in the array, and if so execute code
- How to find duplicate in a list and update the same list
- Find the equal sets of strings and their count (distinct lists)
- Linq Find Partial Text Match - Included code returns duplicate and everything except what it should
- How to check if all list items have the same value and return it, or return an “otherValue” if they don’t?
- Find index of a value in an array
- How do I find the text within a div in the source of a web page using C#
- How do you use LINQ to find the duplicate of a specific property?
- How do I get the index of the highest value in an array using LINQ?
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- using LINQ to find the cumulative sum of an array of numbers in C#
- Take the first five elements and the last five elements from an array by one query using LINQ
More Query from same tag
- turn 2 columns in table to custom string using lambda IN asp.net
- Adding AND in a WHERE clause using ViewBag and SelectList with LINQ in Controller (ASP.Net MVC)
- Lifetime of 'Linqed' IObservable using Reactive Expressions
- The filters navigation Only one unique filter per navigation is allowed
- Unable to create a constant value of type 'Project.Models.Subcategory'. Only primitive types or enumeration types are supported in this context
- LINQ method using Where, Contains and Foreach to update Entity.DbSet using a List<T>
- How to properly (deep) map complex objects with C# LINQ?
- How to use linq extensions methods using Expression.Call
- How to fill object with list using EF
- How can store variable while select query in LINQ?
- GroupBy 'Linq like' in Typescript
- EF Core Linq Join with OR condition using Method Systax
- LINQ: How to get the Max Id with a group by clause?
- Parse XML string to Object with custom namespace
- The cast to value type 'System.Int32' failed because the materialized value is null.
- Challenge: elegantly LINQify this procedural code
- Nested select with group by using linq
- How to sum elements of unknown number of arrays using LINQ?
- Coalescing results in Linq
- Entity Framework - AddOrUpdate using Linq and Lambda
- Getting NullReferenceException from .Select() when .Count() is greater than zero LINQ to XML
- how to convert time field into string using linq sql?
- Linq cannot filter elements in loop
- Convert where clause containing select to Linq
- Group by day, time and id in one LINQ
- Order list of entities which have lists by string length in inner list
- Linq returning specific type
- 'Could not convert from type' Error
- Three trier Linq query
- C# Change Return Value of Linq Expression