score:1
This query does exactly the same as your function, if I'm not mistaken:
var repeated = str.GroupBy(s => s).Where(group => group.Any())
.Select(group =>
{
var indices = Enumerable.Range(1, str.Count).Where(i => str[i-1] == group.Key).ToList();
return string.Join(" - ", group.Select((s, i) => indices[i]));
});
It firstly groups the items of the original list, so that every item with the same content is in a group. Then it searches for all indices of the items in the group in the original list, so that we have all the indices of the original items of the group. Then it joins the indices to a string, so that the resulting format is similiar to the one you requested. You could also transform this statement lambda to an anonymous lambda:
var repeated = str.GroupBy(s => s).Where(group => group.Any())
.Select(group => string.Join(" - ",
group.Select((s, i) =>
Enumerable.Range(1, str.Count).Where(i2 => str[i2 - 1] == group.Key).ToList()[i])));
However, this significantly reduces performance.
I tested this with the following code:
public static void Main()
{
var str = new List<string>
{
"bla",
"bla",
"baum",
"baum",
"nudel",
"baum",
};
var copy = new List<string>(str);
var repeated = str.GroupBy(s => s).Where(group => group.Any())
.Select(group => string.Join(" - ",
group.Select((s, i) =>
Enumerable.Range(1, str.Count).Where(i2 => str[i2 - 1] == group.Key).ToList()[i])));
var repeated2 = Repeat(str);
var repeated3 = str.GroupBy(s => s).Where(group => group.Any())
.Select(group =>
{
var indices = Enumerable.Range(1, str.Count).Where(i => str[i-1] == group.Key).ToList();
return string.Join(" - ", group.Select((s, i) => indices[i]));
});
Console.WriteLine(string.Join("\n", repeated) + "\n");
Console.WriteLine(string.Join("\n", repeated2) + "\n");
Console.WriteLine(string.Join("\n", repeated3));
Console.ReadLine();
}
public static List<string> Repeat(List<string> str)
{
var distinctItems = str.Distinct();
var repeat = new List<string>();
foreach (var item in distinctItems)
{
var added = false;
var reItem = "";
for (var index = 0; index < str.LongCount(); index++)
{
if (item != str[index])
continue;
added = true;
reItem += " - " + (index + 1);
}
if (added)
repeat.Add(reItem.Substring(3));
}
return repeat;
}
Which has the followin output:
1 - 2
3 - 4 - 6
5
1 - 2
3 - 4 - 6
5
1 - 2
3 - 4 - 6
5
score:1
Inside your repeat
method you can use following way to get repeated items
var repeated = str.GroupBy(s=>s)
.Where(grp=>grp.Count()>1)
.Select(y=>y.Key)
.ToList();
Source: stackoverflow.com
Related Articles
- Remove the repeating items and return the order number
- Return distinct list of object array where number of array items is non-specific
- linq - return common items from n number of lists
- Return duplicate items from Custom List<T> not in order C#
- LINQ to entites: How to return the latest order number for a customer
- How do i return a certain max number of items from a collection using linq
- How do I return all order items for all orders?
- How to order the items of a list and remove duplicates based on a property?
- return a list of objects ordered by number of items in navigation property with linq to entity
- Putting an order number on items in a linq query
- How to check if all list items have the same value and return it, or return an “otherValue” if they don’t?
- C# - code to order by a property using the property name as a string
- Linq Order by a specific number first then show all rest in order
- How to Quickly Remove Items From a List
- Best way to remove multiple items matching a predicate from a .NET Dictionary?
- Remove items of list from another lists with criteria
- Remove items from IEnumerable<T>
- compare two list and return not matching items using linq
- LINQ return items in a List that matches any Names (string) in another list
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- Remove items from list that intersect on property using Linq
- How do I count the number of child collection's items using LINQ Method Syntax?
- Find And Remove Items From Collection
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- LINQ: Order By Count of Unique Items in List<string>
- How to Remove multiple items in List using RemoveAll on condition?
- How do I remove items from generic list, based on multiple conditions and using linq
- in C#, how do I order items in a list where the "largest" values are in the middle of the list
- passing dynamic expression to order by in code first EF repository
- LINQ sum collection of items to return object with results (multiple columns)
- How do I update a foreign key efficiently in LINQ to SQL/SQLMetal?
- Group List by property then return only another property of this grouped objects
- In MVC3 how to get a list of models with Linq?
- Return a ReadOnlyDictionary sorted by value
- linq query close my wpf application
- Sitefinity - LINQ to SQL Limitations?
- Does LINQ Select affect on performance?
- How to return specific list result from linq to entities
- Update and replace datatable rows value throughout all columns
- How to group only subsequent items with the same property using linq
- Get count and avg for specific criterias and also the rest
- DataContext accessed after Dispose in render pages/controls
- How to write this in C# Linq
- How to write this linq query?
- Is there a .NET queue class that allows for dequeuing multiple items at once?
- How to improve my LINQ
- How to get a value of a nested class
- Bulk update with LINQ based on list
- Exclude control container from list
- Creating a List<string> off of one of List<Object>'s string properties