How to Count Elements in C# Array?
Last Updated :
23 Jan, 2023
To count the number of elements in the C# array, we can use the count() method from the IEnumerable. It is included in the System.Linq.Enumerable class. The count method can be used with any type of collection such as an array, ArrayList, List, Dictionary, etc.
Syntax:
Count<TSource>()
This method returns the total number of elements present in an array.
Count<TSource>(Func<TSource, Boolean>)
This method returns the total number of elements in an array that matches the specified condition using Func delegate.
Example 1: Using String values in the array.
In the below code block, we have implemented the count function to count the number of elements in a C# Array. Firstly, we have used the System.Linq, because the count function is located in this class, then we have created a count variable to count the number of elements in the array. After that, we have created an array of strings with 6 elements in it. Then we have used the Count function in the array and store the resultant count into the count variable that we have created earlier. Then using the console. Write line we are displaying the count of elements in the array.
C#
// C# program to illustrate the above concept
using System;
using System.Linq;
class GFG{
static public void Main()
{
// Initializing count variable
var totalCount = 0;
// Creating an array of strings
string[] elements = { "Rem", "Hisoka", "Gon",
"Monkey D Luffy", "Alvida",
"Shank" };
// Invoking count function on the above elements
totalCount = elements.Count();
// Displaying the count
Console.WriteLine(totalCount);
}
}
Time Complexity : O(N) , here N is number of elements in string array.
Auxiliary Space : O(1), since no extra space used.
Example 2: Using integer values in the array.
C#
// C# program to illustrate the above concept
using System;
using System.Linq;
class GFG{
static public void Main()
{
// Creating a count variable
var total = 0;
// creating array of numbers
int[] nums = { 9, 6, 5, 2, 1, 5, 8, 4,
6, 2, 3, 4, 8, 7, 5, 6 };
// Counting the number of elements
total = nums.Count();
// Displaying the count
Console.WriteLine(total);
}
}
Time Complexity : O(N) , here N is number of elements in nums array.
Auxiliary Space : O(1), since no extra space used.
Example 3: To count specific elements based on conditions within the array.
Here, in the below program, we are creating an array of colors, and then using the count method, we are finding the number of times the color "Blue" appears in the array. Then displaying the count.
C#
// C# program to illustrate the above concept
using System;
using System.Linq;
class GFG{
static public void Main()
{
// Creating a count variable
var total = 0;
// Creating an array of colors
string[] colors = { "Red", "Blue", "Black",
"White", "Blue", "Blue" };
// Counting the total number of time blue appears
// in the array
total = colors.Count(c => c == "Blue");
// Displaying the count
Console.WriteLine(total);
}
}
Output:
3
Time Complexity : O(N) , here N is number of elements in string array.
Auxiliary Space : O(1), since no extra space used.
Similar Reads
Copying the Queue elements to 1-D Array in C# Queue<T>.CopyTo(T[], Int32) Method is used to copy the Queue elements to an existing one-dimensional Array, starting at the specified array index. The elements are copied to the Array in the same order in which the enumerator iterates through the Queue and this method is an O(n) operation, whe
4 min read
C# | Number of elements contained in the BitArray The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.Count property is used to get the number of element
2 min read
How to create the ArrayList in C# ArrayList() constructor is used to initialize a new instance of the ArrayList class which will be empty and will have the default initial capacity. ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dyn
2 min read
C# | Convert Queue To array Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue.ToArray Method is used to copy the Queue elements to a new array
2 min read
C# | Convert Stack to array Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Stack<T>.ToArray Method is used to copy a Stack<T
2 min read
C# | How to convert an ArrayList to Array In C#, an array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float etc. and the elements are stored in a contiguous location.ArrayList represen
4 min read