using System.Collections.Generic;
public class ArrayOperations
public static void PerformArrayOperations()
Console.WriteLine("Enter the size of the array:");
if (!int.TryParse(Console.ReadLine(), out int size) || size <= 0)
Console.WriteLine("Invalid input. Array size must be a positive integer.");
int[] arr = new int[size];
Console.WriteLine($"Enter {size} elements for the array:");
for (int i = 0; i < size; i++)
if (!int.TryParse(Console.ReadLine(), out int element))
Console.WriteLine("Invalid input. Please enter integers only.");
Console.WriteLine("\nChoose an operation:");
Console.WriteLine("1. Sum");
Console.WriteLine("2. Average");
Console.WriteLine("3. Min");
Console.WriteLine("4. Max");
Console.WriteLine("5. Add");
Console.WriteLine("6. Remove");
Console.WriteLine("7. Find Duplicates");
Console.WriteLine("8. Display Array");
Console.WriteLine("9. Exit");
Console.WriteLine("Enter your choice (1-9):");
if (!int.TryParse(Console.ReadLine(), out int choice))
Console.WriteLine("Invalid input. Please enter a number between 1 and 9.");
Console.WriteLine($"Sum: {CalculateSum(arr)}");
Console.WriteLine($"Average: {CalculateAverage(arr)}");
Console.WriteLine($"Min: {FindMin(arr)}");
Console.WriteLine($"Max: {FindMax(arr)}");
Console.WriteLine("Enter the element to add:");
if (!int.TryParse(Console.ReadLine(), out int elementToAdd))
Console.WriteLine("Invalid input. Please enter an integer.");
Console.WriteLine($"Invalid Input! Please enter a number between 1 and {size}.");
arr = AddElement(arr, elementToAdd);
Console.WriteLine("Element added.");
Console.WriteLine("No elements found in an array.");
Console.WriteLine("Enter the element to remove:");
if (!int.TryParse(Console.ReadLine(), out int elementToRemove))
Console.WriteLine("Invalid input. Please enter an integer.");
arr = RemoveElement(arr, elementToRemove);
catch (ArgumentException ex)
Console.WriteLine(ex.Message);
Console.WriteLine("Element removed.");
Console.WriteLine("Exiting...");
Console.WriteLine("Invalid choice. Please enter a number between 1 and 9.");
if (arr.Length == 0 && choice != 9 && choice != 5)
Console.WriteLine("No elements found in an array.");
Console.WriteLine($"An error occurred: {ex.Message}");
public static int CalculateSum(int[] arr)
if (arr == null || arr.Length == 0)
public static double CalculateAverage(int[] arr)
if (arr == null || arr.Length == 0)
public static int FindMin(int[] arr)
if (arr == null || arr.Length == 0)
public static int FindMax(int[] arr)
if (arr == null || arr.Length == 0)
public static int[] AddElement(int[] arr, int element)
return new int[] { element };
int[] newArr = new int[arr.Length + 1];
Array.Copy(arr, newArr, arr.Length);
newArr[arr.Length] = element;
public static int[] RemoveElement(int[] arr, int element)
if (arr == null || arr.Length == 0)
throw new ArgumentException("No elements found in an array.");
int index = Array.IndexOf(arr, element);
throw new ArgumentException("Invalid Input! There's no such element found to remove.");
int[] newArr = new int[arr.Length - 1];
Array.Copy(arr, 0, newArr, 0, index);
Array.Copy(arr, index + 1, newArr, index, arr.Length - index - 1);
public static void FindDuplicates(int[] arr)
if (arr == null || arr.Length == 0)
Console.WriteLine("No elements found in an array.");
var duplicates = arr.GroupBy(x => x)
.Where(g => g.Count() > 1)
if (duplicates.Count == 0)
Console.WriteLine("No duplicates found.");
Console.WriteLine("Duplicates: " + string.Join(", ", duplicates));
public static void DisplayArray(int[] arr)
if (arr == null || arr.Length == 0)
Console.WriteLine("No elements found in an array.");
Console.WriteLine("Array: " + string.Join(", ", arr));
public class ArrayOperationsTests
public void CalculateSum_ValidArray_ReturnsCorrectSum()
int[] arr = { 1, 2, 3, 4, 5 };
Assert.That(ArrayOperations.CalculateSum(arr), Is.EqualTo(15));
public void CalculateSum_EmptyArray_ReturnsZero()
Assert.That(ArrayOperations.CalculateSum(arr), Is.EqualTo(0));
public void CalculateSum_NullArray_ReturnsZero()
Assert.That(ArrayOperations.CalculateSum(arr), Is.EqualTo(0));
public void CalculateAverage_ValidArray_ReturnsCorrectAverage()
int[] arr = { 1, 2, 3, 4, 5 };
Assert.That(ArrayOperations.CalculateAverage(arr), Is.EqualTo(3));
public void CalculateAverage_EmptyArray_ReturnsZero()
Assert.That(ArrayOperations.CalculateAverage(arr), Is.EqualTo(0));
public void CalculateAverage_NullArray_ReturnsZero()
Assert.That(ArrayOperations.CalculateAverage(arr), Is.EqualTo(0));
public void FindMin_ValidArray_ReturnsCorrectMin()
int[] arr = { 5, 2, 8, 1, 9 };
Assert.That(ArrayOperations.FindMin(arr), Is.EqualTo(1));
public void FindMin_EmptyArray_ReturnsMinValue()
Assert.That(ArrayOperations.FindMin(arr), Is.EqualTo(int.MinValue));
public void FindMin_NullArray_ReturnsMinValue()
Assert.That(ArrayOperations.FindMin(arr), Is.EqualTo(int.MinValue));
public void FindMax_ValidArray_ReturnsCorrectMax()
int[] arr = { 5, 2, 8, 1, 9 };
Assert.That(ArrayOperations.FindMax(arr), Is.EqualTo(9));
public void FindMax_EmptyArray_ReturnsMaxValue()
Assert.That(ArrayOperations.FindMax(arr), Is.EqualTo(int.MaxValue));
public void FindMax_NullArray_ReturnsMaxValue()
Assert.That(ArrayOperations.FindMax(arr), Is.EqualTo(int.MaxValue));
public void AddElement_ValidArray_ReturnsNewArrayWithElementAdded()
int[] expected = { 1, 2, 3, 4 };
Assert.That(ArrayOperations.AddElement(arr, 4), Is.EqualTo(expected));
public void AddElement_NullArray_ReturnsNewArrayWithElementAdded()
Assert.That(ArrayOperations.AddElement(arr, 4), Is.EqualTo(expected));
public void RemoveElement_ValidArray_ReturnsNewArrayWithElementRemoved()
int[] arr = { 1, 2, 3, 4 };
int[] expected = { 1, 3, 4 };
Assert.That(ArrayOperations.RemoveElement(arr, 2), Is.EqualTo(expected));
public void RemoveElement_ElementNotFound_ThrowsArgumentException()
Assert.Throws<ArgumentException>(() => ArrayOperations.RemoveElement(arr, 4), "Invalid Input! There's no such element found to remove.");
public void RemoveElement_NullArray_ThrowsArgumentException()
Assert.Throws<ArgumentException>(() => ArrayOperations.RemoveElement(arr, 4), "No elements found in an array.");
public void RemoveElement_EmptyArray_ThrowsArgumentException()
Assert.Throws<ArgumentException>(() => ArrayOperations.RemoveElement(arr, 4), "No elements found in an array.");
public static void Main()
new NUnitLite.AutoRun().Execute(["--noc" ]);