I have a list consisting of duplicates, I want to create an output showing them in rank. e.g tom at the top because he has 100 entries and lilly at the bottom because it has 0.
is this possible to display a list with a rank corosponding to the number of times they appear in the list?
also display the number of times they appear next to them?
The following will give an IEnumerable where each item contains a representative item from the group and the number of times it occurs.
myList
.GroupBy( item => item.Name )
.OrderByDescending( g => g.Count() )
.Select( g => new {item=g.First(), numOcurrences=g.Count() )
var query = File
.ReadLines("input.txt")
.GroupBy(x => x)
.Select(g => new { Key = g.Key, Count = g.Count() })
.OrderByDescending(i => i.Count)
.Take(20);
foreach (var item in query)
{
Console.WriteLine("{0,5} {1}", item.Count, item.Key);
}
var groups = list.GroupBy(item => item);
var orderedGroups = groups.OrderByDescending(group => group.Count());
foreach (var group in orderedGroups)
{
Console.WriteLine(string.Format("{0} - {1}", group.Key, group.Count());
}
Here's a simple LINQ query that does remotely that.
List<string> list = new List<string> { "a", "b", "a", "c", "a", "d", "c" };
var counts = from item in list
group item by item
into grp
orderby grp.Count() descending
select new
{
Value = grp.Key,
Count = grp.Count()
};
foreach (var item in counts)
Console.WriteLine("{0} ({1})", item.Value, item.Count);
Related
want to check if list contains same items
var listme = new List<string>();
listme.Add("list1");
listme.Add("list1");
And want to count the number of same items and copy it and then remove it from list.
You can do it in a single LINQ statement with GroupBy and ToDictionary:
var dupCounts = listme
.GroupBy(s => s)
.Where(g => g.Count() > 1) // Keep only groups with duplicates
.ToDictionary(g => g.Key, g => g.Count());
This produces a Dictionary<string,int> where each item from the list is mapped to its corresponding count from the original list. Now you can remove all duplicates from the original list:
listme.RemoveAll(dupCounts.Keys);
Try to use HashTable instead of List
Hashtable hashtable = new Hashtable();
hashtable[1] = "One";
hashtable[2] = "Two";
hashtable[13] = "Thirteen"
You can use linq, see below:
public static void Main()
{
var listme = new List<string> {"A", "A", "B", "C", "C"};
// count
var countDict = listme.GroupBy(i => i)
.ToDictionary(i => i.Key, i => i.Count());
foreach (var kv in countDict)
{
Console.WriteLine($"{kv.Key}: {kv.Value}");
}
// remove
listme.RemoveAll(s => s == "A");
foreach (string s in listme)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
I have a list of a list of strings:
List<List<String>> pChain;
It might have repeated lists of strings (two list of strings are equal if they have the same strings in the same order). I want to have the count of each distinct list in the main list. I tried:
var results = (from t in pChain
group t by new { t }
into g
select new
{
g.Key,
Count = g.Count(),
}).OrderByDescending(x => x.Count).ToList();
foreach (var v in results)
{
ListViewItem lv = listView2.Items.Add(v.Key.ToString());
lv.SubItems.Add(v.Count + "");
}
But it doesn't group similar list of strings into one list and doesn't count them.
You can use SelectMany + Distinct:
var allDistinctItems = pChain.SelectMany(list => list).Distinct();
If you want the count use int countOfDistinctItems = allDistinctItems.Count();.
If you want a dictionary you could use:
Dictionary<string, int> itemCounts = pChain.SelectMany(list => list)
.GroupBy(item => item)
.ToDictionary(g => g.Key, g => g.Count());
You can check if a list of lists contains an specific list by iterating through its elements and checking if they are SequenceEqual(). You should be able to remove the duplicate lists with this:
for(int i = 0; i < pChain.Count(); i++)
{
// If the amount(Count) of SequenceEqual lists in pChain for the current iteration
// of pChain (pChain[i]) is > 1
if (pChain.Count(l => l.SequenceEqual(pChain[i])) > 1)
pChain.RemoveAt(i);
}
Thus the amount of distinct lists would be:
int count = pChain.Count();
You can put the code above into a single linQ line this way:
pChain.Select((x, y) => new { list = x, Index = y }).ToList()
.ForEach(l1 => {
if (pChain.Count(l2 => l2.SequenceEqual(l1.list)) > 1)
pChain.RemoveAt(l1.Index);
});
I tried Aggregate function to join the strings of the inner list to a string resulted from concatenating them. Then applied the GroupBy to this list.
Dictionary<string, int> itemCounts =
pChain.Select(list => list.Aggregate((i, j) => j + '/' + i))
.GroupBy(item => item).OrderByDescending(x => x.Key)
.ToDictionary(g => g.Key.ToString(), g => g.Count());
foreach (var v in itemCounts)
{
ListViewItem lv = listView2.Items.Add(v.Key.ToString());
lv.SubItems.Add(v.Value + "");
}
I have a list of model class with some fields in it,I want to apply group by and count on the base of ID on it,in my lst I have data like this
lst[0]=Id=10,Name="user1",Gender="Male",Course="course1";
lst[1]=Id=10,Name="user1",Gender="Male",Course="course2";
lst[2]=Id=10,Name="user1",Gender="Male",Course="course3";
lst[3]=Id=11,Name="user2",Gender="Male",Course="course4";
I want to count the course with group by on Id.
This is how I am doing
var result = lst.GroupBy(n => n.Id).Select(c => new { Key = c.Key, total = c.Count()});
But after applying this I am not getting any value in result variable(ie result.Name or result[0].Name)
How can I apply group by and count in this list so I get the list data after applying the group by too.
Having used GroupBy you have projected (Select(..)) to an anonymous object which throws away the selected items in the group; your result will be an IEnumerable<anonymous> of items with properties Key and total
var result = lst.GroupBy(n => n.Id)
.Select(c => new { Key = c.Key, total = c.Count()});
If you want to keep the selected items, you need to include this in your projection
var result = lst.GroupBy(n => n.Id)
.Select(c => new { Key = c.Key, total = c.Count(), Items = c});
foreach(var r in result)
{
Console.WriteLine("Key: {0} Count:{1}",r.Key,r.total)
foreach(var i in r.Items)
Console.WriteLine(i.Name);
}
The reason is because you are grouping by Id and just provinding an anonymous object with Key (which is the Id) and a Count. If you want to read the values, each item of the result group set will have the Count and the values where you can loop between them. For sample:
var result = lst.GroupBy(n => n.Id);
foreach (var g in result)
{
Console.WriteLine("For group {0} the total is {1}.", g.Key, g.Count());
foreach(var item in g)
{
Console.WriteLine("Name: {0}", item.Name);
}
}
All you have to do is call ToList or ToArray.
var result = lst.GroupBy(n => n.Id)
.Select(c => new
{
Key = c.Key,
total = c.Count(),
Gender = c.First().Gender
}).ToList();
Or alternativly - to avoid multiple iterations of the same enumeration:
var result = lst.GroupBy(n => n.Id)
.Select(c =>
{
var firstEl = c.First();
return new {
Key = c.Key,
total = c.Count(),
Gender = firstEl.Gender,
Name = firstEl.Name,
Course = firstEl.Course
}
}).ToList();
Select will only return a query which is deferredly executed. This means you get the result when you actually iterate on the enumeration, which is enforeced by calling one of the mentioned methods.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to get the count for the most common duplicate item in a list.
So far, I have:
List<string> brandList = new List<string>();
which contains 5 different soft drink brands, and the count is 100. I need to find out which brand has the most duplicates in the list and count how many duplicates there are.
Presuming that your pseudo code actually was:
List<Brand> brandList=new List<Brand>();
// fill list
and your Brand class either overrides Equals+getHashCode or has a property like BrandID which is the identifier. N youow want to get the count of the most popular brand, you can use LINQ:
var mostPopularBrand = brandList.GroupBy(b => g.BrandID)
.OrderByDescending(g => g.Count())
.Select(g => new { BrandID = g.Key, Count = g.Count()})
.First();
Console.WriteLine("Most poular brand: {0} Count: {1}",
mostPopularBrand.BrandID, mostPopularBrand.Count);
Update: If it's actually a List<string>(question was edited):
var mostPopularBrand = brandList.GroupBy(str => str)
.OrderByDescending(g => g.Count())
.Select(g => new { Brand = g.Key, Count = g.Count()})
.First();
Your code doesn't compile. Providing that you mean List<String> as a storage of brands:
var ListbrandList = new List<String>() {
"Cola",
"Juice",
"Cola",
"Water",
"Milk",
"Water",
"Cola",
};
var result = ListbrandList
.GroupBy(item => item)
.Select(item => new {
Name = item.Key,
Count = item.Count()
})
.OrderByDescending(item => item.Count)
.ThenBy(item => item.Name);
String report = String.Join(Environment.NewLine, result
.Select(item => String.Format("{0} appears {1} time(s)", item.Name, item.Count)));
you'll have report as
Cola appears 3 time(s)
Water appears 2 time(s)
Juice appears 1 time(s)
Milk appears 1 time(s)
var result = brandList
.Distinct()
.GroupJoin(brand,
k => k,
b => b,
(k, b) => new { BrandName = k, Count = b.Count() });
// An usage could be...
foreach (var r in result)
{
Debug.WriteLine("{0} Brand has {1}", r.BrandName, r.Count);
}
Without LinQ:
var result = new Dictionary<string, int>();
foreach (var brand in brandList)
{
if (!result.ContainsKey(brand))
{
var count = brandList.FindAll(x => x.Equals(brand)).Count;
result.Add(brand, count);
}
}
foreach (var r in result)
{
Console.WriteLine("{0} Brand has {1}", r.Key, r.Value);
}
You can try to do group using LINQ by Elements and calculate count
var groupedList = from l in ListbrandList
group l by l into grp
select new { key = grp.Key, cnt = grp.Count()};
Then you will have group by key (Element) and value (Count)
Something like this maybe:
var res = brandList.GroupyBy(x => x)
.Select(x => new {Key = x.Key, Count = x.Count});
Now you have a list containing the actual drink-number and the count for this number.
EDIT: Getting the brand with most count is now easy, e.g. by using:
var mostPopular = res.Single(x => x.Count == res.Max(y => y.Count));
EDIT2: Without LINQ the following might work, which is way longer and more complicated:
// get the count for every brand
Dictionary<string, int> res = new Dictionary<string, int>();
foreach(var x in brands)
{
if (!res.ContainsKey(x)) res[x] = 0;
res[x]++;
}
// now get the max count
int currentMax = 0;
string key = "";
foreach (var kv in res)
{
if (kv.Value > currentMax)
{
currentMax = kv.Value;
key = kv.Key;
}
}
Now the key should contain the brand with highest Count.
To count element with specific value in a list use :
int quantity = lst.Count(r => r == "Cola");
Example :
List<string> lst = new List<string>()
{
"Sprite",
"Cola",
"Sprite",
"Sprite",
"Cola",
"Sprite",
"Sprite",
"Cola",
"Pepsi",
"Sprite",
"Pepsi",
"Sprite",
};
string[] popularBrands = { "Cola", "Pepsi" };
int[] quantities = new int[popularBrands.Length];
for (int i = 0; i < popularBrands.Length; i++)
{
quantities[i] = lst.Count(r => r.ToUpper() == popularBrands[i].ToUpper());
Console.WriteLine("{0} : {1}", popularBrands[i], quantities[i]);
}
Output
Cola : 3
Pepsi : 2
P.S.
About this code r => r.ToUpper() == popularBrands[i].ToUpper() :
r is variable that holds a value from our list (that are taken one by one). We also use ToUpper() to make sure that our check is case insensitive.
So we basically loop through the collection taking values out of it one by one. Each time we put value to r variable and check if this variable satisfies condition. If so - we count it, if not - we just move to next value.
I have a very simple List<string> setup which contains lots of single characters per item (IE a foreach would console out to "a" "k" "p" etc)
What I'd like to do is be able to group the items and also count how many of each occurs so I'd get an output similar to:
a - 2
t - 3
y - 3
Any tips on the best way to do this?
I am using .Net 4 if that's any help.
(Given that each entry is a single character, is there any reason you don't have a List<char> by the way?)
How about:
// To get a Dictionary<string, int>
var counts = list.GroupBy(x => x)
.ToDictionary(g => g.Key, g => g.Count());
// To just get a sequence
var counts = list.GroupBy(x => x)
.Select(g => new { Text = g.Key, Count = g.Count() });
Note that this is somewhat inefficient in terms of internal representation. You could definitely do it more efficiently "manually", but it would also take more work. Unless your list is large, I would stick to this.
The easiest way to do this is the Linq using
var list = new[] { "a", "a", "b", "c", "d", "b" };
var grouped = list
.GroupBy(s => s)
.Select(g => new { Symbol = g.Key, Count = g.Count() });
foreach (var item in grouped)
{
var symbol = item.Symbol;
var count = item.Count;
}
var list = new[] {"a", "t", "t", "y", "a", "y", "y", "t"};
var result = (from item in list
group item by item into itemGroup
select String.Format("{0} - {1}", itemGroup.Key, itemGroup.Count()));