Arrays are among the most fundamental data structures in any programming language, including C#. They are essential for storing collections of elements and are widely used in both academic and industrial programming scenarios. Due to their importance, arrays are frequently featured in technical interviews and coding assessments. In this document, weβll cover key concepts, common array-based problems, their C# implementations, and frequently asked interview questions.
An array in C# is a fixed-size, strongly-typed collection of elements. It is used to store multiple values of the same type in a single variable instead of declaring separate variables for each value.
// Declaration
int[] numbers = new int[5];
// Initialization
numbers[0] = 10;
numbers[1] = 20;
// Combined
int[] nums = { 1, 2, 3, 4, 5 };
int[] arr = { 2, 5, 1, 7, 3 };
int max = arr[0];
for (int i = 1; i < arr.Length; i++)
{
if (arr[i] > max)
max = arr[i];
}
Console.WriteLine("Maximum element: " + max);
int min = arr[0];
for (int i = 1; i < arr.Length; i++)
{
if (arr[i] < min)
min = arr[i];
}
Console.WriteLine("Minimum element: " + min);
int[] arr = { 1, 2, 3, 4, 5 };
Array.Reverse(arr);
foreach (int i in arr)
{
Console.Write(i + " ");
}
int[] arr = { 4, 2, 8, 1, 3 };
Array.Sort(arr);
foreach (int i in arr)
{
Console.Write(i + " ");
}
int[] arr = { 2, 4, 6, 8 };
int sum = 0;
foreach (int i in arr)
{
sum += i;
}
double average = (double)sum / arr.Length;
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Average: " + average);
int[] source = { 10, 20, 30 };
int[] destination = new int[source.Length];
Array.Copy(source, destination, source.Length);
int[] a = { 1, 2, 3 };
int[] b = { 4, 5 };
int[] merged = new int[a.Length + b.Length];
a.CopyTo(merged, 0);
b.CopyTo(merged, a.Length);
int[] arr = { 1, 2, 2, 3, 4, 4, 5 };
int[] unique = arr.Distinct().ToArray();
var duplicates = arr
.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(g => g.Key);
int search = 7;
bool found = arr.Contains(search);
Console.WriteLine(found ? "Found" : "Not Found");
int[] arr = { 1, 2, 3, 4, 5 };
int k = 2;
for (int i = 0; i < k; i++)
{
int temp = arr[0];
for (int j = 0; j < arr.Length - 1; j++)
{
arr[j] = arr[j + 1];
}
arr[arr.Length - 1] = temp;
}
int[] arr = { 1, 2, 2, 3, 3, 3 };
var frequency = arr.GroupBy(x => x)
.ToDictionary(g => g.Key, g => g.Count());
foreach (var item in frequency)
{
Console.WriteLine($"{item.Key} occurs {item.Value} times");
}
int[] arr = { 10, 20, 4, 45, 99 };
int first = int.MinValue;
int second = int.MinValue;
foreach (int num in arr)
{
if (num > first)
{
second = first;
first = num;
}
else if (num > second && num != first)
{
second = num;
}
}
Console.WriteLine("Second Largest: " + second);
int[] arr = { 1, 5, 7, -1, 5 };
int sum = 6;
for (int i = 0; i < arr.Length; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] + arr[j] == sum)
{
Console.WriteLine($"Pair: ({arr[i]}, {arr[j]})");
}
}
}
Array.Copy(source, destination, length) copies elements from the source array to the destination array. It performs a shallow copy.
A jagged array is an array of arrays, where each sub-array can have a different length.
int[][] jagged = new int[2][];
jagged[0] = new int[] { 1, 2 };
jagged[1] = new int[] { 3, 4, 5 };
Arrays are reference types, so the reference is stored on the stack and the array object itself on the heap. The array memory is contiguous for single-dimensional arrays.
You canβt change the size of an existing array, but you can create a new one and copy elements using Array.Resize().
Yes, arrays support LINQ queries. For example:
int[] arr = { 1, 2, 3, 4 };
var even = arr.Where(x => x % 2 == 0).ToArray();
One-dimensional arrays store elements linearly. Multi-dimensional arrays store elements in grid-like format.
int[] original = { 1, 2, 3 };
int[] clone = (int[])original.Clone();
bool exists = arr.Contains(5);
Arrays form the backbone of many C# programs, from simple data collection to complex algorithms. Mastery of array operations, patterns, and problem-solving approaches is vital for any developer aiming to crack coding interviews or build efficient applications. This document covered not only the syntax and logic behind array handling in C#, but also offered a wealth of practical programs and frequently asked questions to guide your preparation.
Continue practicing problems, try modifying and optimizing solutions, and understand how memory and performance are affected by different types of arrays. This foundational knowledge will help you throughout your software development journey.
C# is primarily used on the Windows .NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved