0% found this document useful (0 votes)
36 views2 pages

CSharp Array QA

The document provides a comprehensive guide on working with integer arrays in C#, covering topics such as declaration, initialization, looping, finding maximum and minimum values, sorting, summing elements, checking existence, removing duplicates, reversing, resizing, and finding duplicates. Each section includes code examples demonstrating the respective operations. This serves as a practical reference for developers dealing with integer arrays in C#.

Uploaded by

Sahil Rafeeq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views2 pages

CSharp Array QA

The document provides a comprehensive guide on working with integer arrays in C#, covering topics such as declaration, initialization, looping, finding maximum and minimum values, sorting, summing elements, checking existence, removing duplicates, reversing, resizing, and finding duplicates. Each section includes code examples demonstrating the respective operations. This serves as a practical reference for developers dealing with integer arrays in C#.

Uploaded by

Sahil Rafeeq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C# int[] Array Related Questions and Answers

1. How to declare and initialize an int[] array in C#?


int[] numbers = new int[5]; // default values = 0
int[] values = new int[] { 1, 2, 3, 4, 5 };
int[] items = { 10, 20, 30 };

2. How to loop through an integer array?


int[] nums = { 1, 2, 3, 4, 5 };

foreach (int num in nums)


{
Console.WriteLine(num);
}

// or using for loop


for (int i = 0; i < nums.Length; i++)
{
Console.WriteLine(nums[i]);
}

3. How to find the maximum and minimum value in an int[] array?


int[] arr = { 3, 7, 1, 9, 5 };
int max = arr.Max();
int min = arr.Min();
Console.WriteLine($"Max: {max}, Min: {min}");

4. How to sort an integer array in ascending and descending order?


int[] arr = { 4, 2, 9, 1 };
Array.Sort(arr); // Ascending
Console.WriteLine(string.Join(", ", arr)); // 1, 2, 4, 9
Array.Reverse(arr); // Now descending
Console.WriteLine(string.Join(", ", arr)); // 9, 4, 2, 1

5. How to sum all elements of an int[] array?


int[] arr = { 1, 2, 3, 4, 5 };
int total = arr.Sum();
Console.WriteLine($"Sum = {total}");

6. How to find the second largest number in an integer array?


int[] arr = { 10, 20, 4, 45, 99 };
int secondLargest = arr.OrderByDescending(x => x).Skip(1).First();
Console.WriteLine($"Second largest: {secondLargest}");

7. How to check if a number exists in the array?


int[] arr = { 2, 4, 6, 8 };
bool exists = arr.Contains(6);
Console.WriteLine($"6 Exists? {exists}");

8. How to remove duplicates from an array?


int[] arr = { 1, 2, 2, 3, 4, 4 };
int[] unique = arr.Distinct().ToArray();
Console.WriteLine(string.Join(", ", unique));

9. How to reverse an integer array?


int[] arr = { 1, 2, 3, 4 };
Array.Reverse(arr);
Console.WriteLine(string.Join(", ", arr));

10. How to find the index of an element in the array?


int[] arr = { 5, 10, 15, 20 };
int index = Array.IndexOf(arr, 15);
Console.WriteLine($"Index of 15: {index}");

11. How to resize an array (increase length)?


int[] arr = { 1, 2, 3 };
Array.Resize(ref arr, 5);
arr[3] = 4;
arr[4] = 5;
Console.WriteLine(string.Join(", ", arr));

12. Real-time example: Find all even numbers in an array


int[] arr = { 1, 2, 3, 4, 5, 6 };
var evens = arr.Where(x => x % 2 == 0).ToArray();
Console.WriteLine("Even Numbers: " + string.Join(", ", evens));

13. Find duplicate elements in the array


int[] arr = { 1, 2, 3, 2, 4, 4, 5 };
var duplicates = arr.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(g => g.Key);
Console.WriteLine("Duplicates: " + string.Join(", ", duplicates));

You might also like