# Array Counting • ### Question

• User1194139608 posted

i have array in which render particulare value two or three time m want to get exactly value in array..

string[] arr = new string[]
{
"A",
"A",
"A",

"B" ,

"B"
};

can with help of linq get A render 3 times and B render 2 Times How

Wednesday, June 5, 2013 10:52 AM

• User281315223 posted

I'm not sure exactly the output that you are looking for (a bit more detail would be helpful if possible).

If you want to actually output these values, you could use a basic loop to iterate through your collection and output according :

```//Your string array
string[] arr = new string[]{ "A", "A", "A", "B", "B" };

//Iterate through the collection
foreach(string s in arr)
{
//Very basic example writing out each character
Response.WriteLine(s);
}```

If you want the actual number of occurances of each value within your array, you could use the following LINQ statements that involve the GroupBy() and Select() methods :

```string[] arr = new string[] { "A", "A", "A", "B", "B" };
//Groups your Array and then selects based on the Key and Count per Grouping
//yields { Key = "A", Count = 3 },{ Key = "B", Count = 2 }
var grouped = arr.GroupBy(a => a).Select(g => new { g.Key, Count = g.Count()}); ```

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Wednesday, June 5, 2013 7:32 PM

### All replies

• User163270948 posted

Are you wanting to count the number of A and B in your array uqing Linq?

If so you can group the strings and generate a cound like this:

```var arrCount = from s in arr
group s by s
into grouped
select new
{
Value = grouped.Key,
Count = grouped.Count()
};```

This will give you a collection back where "Value" will be A or B and "Count" will be the number of occurances in the array.

Wednesday, June 5, 2013 1:02 PM
• User-1910946339 posted

Can you give an example of the result you would like to get?

Wednesday, June 5, 2013 7:06 PM
• User281315223 posted

I'm not sure exactly the output that you are looking for (a bit more detail would be helpful if possible).

If you want to actually output these values, you could use a basic loop to iterate through your collection and output according :

```//Your string array
string[] arr = new string[]{ "A", "A", "A", "B", "B" };

//Iterate through the collection
foreach(string s in arr)
{
//Very basic example writing out each character
Response.WriteLine(s);
}```

If you want the actual number of occurances of each value within your array, you could use the following LINQ statements that involve the GroupBy() and Select() methods :

```string[] arr = new string[] { "A", "A", "A", "B", "B" };
//Groups your Array and then selects based on the Key and Count per Grouping
//yields { Key = "A", Count = 3 },{ Key = "B", Count = 2 }
var grouped = arr.GroupBy(a => a).Select(g => new { g.Key, Count = g.Count()}); ```