score:3
You could try this, although I have not performance tested it yet:
List<string> originalList = new List<string>()
{
@"AAA\BBB",
@"AAA\CCC",
@"AAA\CCC",
@"BBB\XXX",
@"BBB",
@"BBB\XXX",
@"BBB\XXX"
};
List<string> outputList = new List<string>();
foreach(var g in originalList.GroupBy(x => x).Select(x => x.ToList()))
{
var index = 1;
foreach(var item in g)
{
outputList.Add(string.Format("{0}[{1}]", item, index++));
}
}
Fiddle here
score:0
You could just use Group() to pull the strings together and then project those groups using a combination of value and count.
Given your list of strings:
var listOfStrings;
var grouped = listOfStrings.GroupBy(x => x);
var groupedCount = grouped.Select(x => new {key = x.Key, count = group.Count()});
score:10
Since you ask for fastest, the best IMO would be to use foreach
loop and counting Dictionary<string, int>
. It has the same time complexity as HashSet
and uses much less memory than LINQ GroupBy
:
var counts = new Dictionary<string, int>(pathList.Count); // specify max capacity to avoid rehashing
foreach (string item in pathList)
{
// Do some stuff here and pick 'item' only if it fits some criteria.
if (IsValid(item))
{
int count;
counts.TryGetValue(item, out count);
counts[item] = ++count;
duplicateItems.Add(item + "[" + count + "]");
}
}
score:1
What about this?
static IEnumerable<string> MyCounter(IEnumerable<string> data)
{
var myDic = new Dictionary<string, int>();
foreach (var d in data)
{
if (!myDic.ContainsKey(d))
myDic[d] = 1;
else
myDic[d] = myDic[d] + 1 ;
yield return d +"[" + myDic[d] + "]";
}
}
score:1
You could iterate over the list and use a dictionary to get the count, like this:
private int GetCount(IDictionary<string, int> counts, string item)
{
int count;
if (!counts.TryGetValue(item, out count))
count = 0;
count++;
counts[item] = count;
return count;
}
private IEnumerable<string> GetItems(IEnumerable<string> items)
{
// Initialize dict for counts with appropriate comparison
var counts = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
foreach(var item in items)
yield return string.Format("{0}[{1}]", item, GetCount(counts, item));
}
score:0
You can use this crisp and crunchy code:
public static void Main()
{
var originalList = new List<string>()
{
@"AAA\BBB",
@"AAA\CCC",
@"AAA\CCC",
@"BBB\XXX",
@"BBB",
@"BBB\XXX",
@"BBB\XXX"
};
var outputList = originalList.GroupBy(x => x).SelectMany(x => x.Select((y, i) => string.Format("{0}[{1}]", y, i + 1)));
Console.WriteLine(string.Join("\n", outputList));
}
score:0
Using a HashSet
Note: Dump() is a LinqPad method that prints the results to the screen — substitute as necessary.
void Main()
{
var list = new List<string> {"hello", "doctor", "name", "continue", "yesterday", "tomorrow", "HELLO"};
//case-insensitive string compare
list.HasDuplicates(StringComparer.OrdinalIgnoreCase).Dump();
//case-sensitive string compare
list.HasDuplicates().Dump();
//integer compare
var list2 = new List<int> { 1,2,3,4,5,2 };
list2.HasDuplicates().Dump();
}
public static class Test
{
public static bool HasDuplicates<T>(this IList<T> list, StringComparer stringComparer = null)
{
if (typeof(T) == typeof(string))
{
var hash = new HashSet<string>(list.Count, stringComparer);
foreach (var val in list) if (!hash.Add(val?.ToString())) break;
return hash.Count != list.Count;
}
else
{
var hash = new HashSet<T>(list.Count);
foreach (var val in list) if (!hash.Add(val)) break;
return hash.Count != list.Count;
}
}
}
/*
output:
True
False
True
*/
Source: stackoverflow.com
Related Articles
- Fastest way to find duplicate items in a list in C#
- Find duplicate items in list based on particular member value
- How to get duplicate items from a list using LINQ?
- LINQ query to find if items in a list are contained in another list
- How to find List has duplicate values in List<string>
- LINQ - Find all items in one list that aren't in another list
- Find items from a list which exist in another list
- Find all items whose collection property contains items in another list
- How to generate duplicate items in a list using LINQ?
- How to find duplicate items in list<>?
- Create duplicate items in a list
- How to find duplicate items based on multiple values using LINQ?
- remove duplicate items from list in c#
- LINQ Join to find items NOT IN a list
- Find duplicate in a list from a reference list
- C# Linq - Find Duplicate value in list and select it's id
- LINQ: find items in a list that have frequency = 1
- Find Duplicate in list but with criteria
- Count of duplicate items in a C# list
- how to get duplicate items from a list in vb.net
- Find indices of particular items in the list using linq
- Fastest way to get matching items from two list c#
- FIND items in list using lambda expression
- C# list -Eliminate Duplicate items by comparing specific properties
- linq query to find distinct combinations of list items taking 2 at a time c#
- Using Linq to group by multiple columns in a list and mark all duplicate items
- Find items with duplicate values over mutiple properties
- Search all List items for specific duplicate and non duplicate values
- Find common items in list of lists of strings
- Duplicate items in a list
- LINQ to XML gets no data with schema set
- interface design with Linq2Nibernate and IQueryable
- How to get current date module name with Linq in C#?
- How to use a new select on the attribute of the first select?
- Sorting optimization for date
- How to detect IsNull / NotNull when building dynamic LINQ expressions?
- Concatenate EF entity nested list titles
- Help needed on SQL query to Linq Conversion
- LINQ Join gives only first result multiple times
- C# Linq query which joins two tables in to one?
- How to select records where field is missing from an Azure Storage Table?
- Create a dictionary by filtering two lists with LINQ
- IQueryable Not Returning Associated Data
- Linq Multiple Joins to SelectList
- SQL query to Lambda Expression C#
- Linq find all with certain type
- Dynamic.Linq Building generic Where Condition
- C# Linq Select two tables select MAX date from one
- Keep throwing an error and I do not know why
- C# Linq Join Lambda (a key selector contains the other key selector)