score:2
Accepted answer
A bit complicated but working:
int occ = 1; // 1 if you want the 2nd occurrence, 2 if you want the 3rd etc...
var nRepeatingElements = list.Select((x, idx) => new { Index = idx, Value = x })
.GroupBy(x => x.Value)
.Where(g => g.Count() > occ)
.Select(x => x.ElementAt(occ))
.ToList();
It returns a list with:
[Index:2 , Value: 'A']
[Index:5 , Value: 'C']
N.B.
The index is 1 lower than your desired solution, (because index is 0-based). But it's really easy to increment it, even in the first LINQ Select if you want:
Select((x, idx) => new { Index = idx+1, Value = x })
score:1
IEnumerable<Item> result =
from item in items
group item by item.Key into g
let secondItem = g.Skip(1).Take(1) //.ToList()
where secondItem.Any()
select secondItem.First()
The ToList call is an optimization to prevent secondItem from being evaluated from g twice. It probably doesn't matter since we're only skipping 1, but it might matter if we start skipping 100.
Here's another solution using Where and a captured variable.
HashSet<Item.Key> seenIt = new HashSet<Item.Key>();
HashSet<Item.Key> returnedIt = new HashSet<Item.Key>();
IEnumerable<Item> result =
items.Where(item =>
{
key = Item.Key;
if (!seenIt[key])
{
seenIt.Add(key);
}
else if (!returnedIt[key])
{
returnedIt.Add(key)
return true;
}
return false;
});
Source: stackoverflow.com
Related Articles
- How to get the second repeated item from a collection of objects using LINQ to object
- Using Linq to select x amount of objects from an observable collection
- Get the item from a collection that contains another collection using linq
- How to remove each item that appears in first collection from second collection using linq?
- Best way to obtain objects from a collection that can be cast to a specific type using Linq
- Update all objects in a collection using LINQ
- How to remove duplicates from collection using IEqualityComparer, LinQ Distinct
- Using LINQ to Objects to find items in one collection that do not match another
- How do I get the latest date from a collection of objects using LINQ?
- Remove item from list using linq
- How to add an item in a collection using Linq and C#
- Select object from nested collection using Linq
- Remove a specific item from a list using LINQ
- Using LINQ to find all keys from one collection that are not in another?
- How to retrieve grandchild objects from a parent using linq
- using LINQ how can i concatenate string properties from itesm in a collection
- How to update a single item of liist of objects using LINQ in C#
- LINQ query to create a collection of objects combined from a collection of strings
- Using LINQ to delete an element from a ObservableCollection Source
- Using LINQ to get the results from another LINQ collection
- Update all objects except one in a collection using Linq
- Best way to assign a value to a property of all objects in a collection using LINQ
- LINQ - C# - Using lambda - Get a set of data from a Collection
- Remove an item from a LINQ collection
- Query in returning Objects from List using LINQ
- LINQ How to combine second item in Tuple after query using where on first item
- using Linq to generate a collection of things to be removed from another collection
- Using LINQ how do I create a List of one particular field of an entity from a collection entities
- Finding objects which contains at least all elements from subset using RavenDB and LINQ
- Replace a collection item using Linq
- Remove items from list where not at *all* locations
- LINQ - Left join and Group by and Sum
- Make a Query Expression ignore the Time and filter only the Date
- How to group by date time in c# for a specific range?
- Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.Data.IDataReader'
- Union with LINQ to XML
- How fast is LINQ?
- Programmatically Add Row to Linq-to-SQL Dataset
- Linq dynamic select casting issue
- Change Value of nested node
- query entity with linq passing in two parameters
- C# - SQL - LINQ Query
- LINQ select one bool from condition over multiple rows
- Translate SQL query into Linq
- The source contains no DataRows
- need to do a subquery in Linq
- Linq, array.Contains() generating exception: Only primitive types ('such as Int32, String, and Guid') are supported in this context
- LINQ to XML And Distinct Custom Class
- LINQ query returning null results
- Why is the Where clause in this LINQ Query not appending?