Q:

number of distinct object types in a list

I am using C#, Silverlight, Visual Studio for Windows Phone 7.
I currently have a List that contains the generic UIElement, and I can put things like TextBlock or Grid or StackPanel into the List.
For example:
List<UIElement> UIList= new List<UIelement>();
UIList.Add(someUIObject as UIElement);

My question is, is there an efficient way to count the number of object types in my list? For example, if there are 8 TextBlocks and 4 Grids, I would like to know that there are 2 object types in the List. Or if there is 1 TextBlock, 1 Grid, and 1 StackPanel, I would like to know that there are 3 types of objects.
I'm looking for something that is better than O(n^2) performance. My current solution compares each element type to the rest of the element types in the List, something similar to BubbleSort.

A:

To get the number of different types in the collection, I would use LINQ to first select the type of each object, then took only distinct types and counted those:
int numberOfTypes = UIList.Select(x => x.GetType()).Distinct().Count();

All of this will be O(n), because Distinct() uses a hash table.

A:

try out
var loader = loaders.OfType<Elementtype>().Count(); 

A:

var types = UIList.GroupBy(ui => ui.GetType())
        .Select(g => new { Type = g.Key, Count = g.Count() })
        .ToList();

