Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Remove all even numbers from an array in C#
Removing all even numbers from an array is a common programming task that involves filtering out numbers divisible by 2. In C#, we can accomplish this using various approaches such as iterative methods, LINQ, or recursion to create a new array containing only odd numbers.
Problem Description
We are given an array and need to remove all the even numbers from it. The resulting array should only contain odd numbers. An even number is any integer that is divisible by 2 (i.e., number % 2 == 0), while an odd number leaves a remainder of 1 when divided by 2.
Example 1
- Input: array = {1, 2, 3, 4, 5, 6}
- Output: {1, 3, 5}
Explanation
The even numbers (2, 4, 6) are removed, leaving only the odd numbers.
Example 2
- Input: array = {12, 15, 19, 22, 27}
- Output: {15, 19, 27}
Explanation
The even numbers (12, 22) are removed, leaving only the odd numbers.
Using Iterative Approach with List
This is a straightforward approach that iterates through the array and adds only odd numbers to a new list, which is then converted back to an array
using System;
using System.Collections.Generic;
class Program {
static int[] RemoveEvenNumbers(int[] array) {
List<int> oddNumbers = new List<int>();
foreach (int number in array) {
if (number % 2 != 0) { // Check if number is odd
oddNumbers.Add(number);
}
}
return oddNumbers.ToArray();
}
static void Main() {
int[] array = { 1, 2, 3, 4, 5, 6 };
int[] result = RemoveEvenNumbers(array);
Console.WriteLine("Original array: [" + string.Join(", ", array) + "]");
Console.WriteLine("After removing even numbers: [" + string.Join(", ", result) + "]");
}
}
The output of the above code is
Original array: [1, 2, 3, 4, 5, 6] After removing even numbers: [1, 3, 5]
Time Complexity: O(n)
Space Complexity: O(n)
Using LINQ Where Method
LINQ provides a concise way to filter arrays using the Where method. This approach is more functional and readable
using System;
using System.Linq;
class Program {
static int[] RemoveEvenNumbersLinq(int[] array) {
return array.Where(number => number % 2 != 0).ToArray();
}
static void Main() {
int[] array = { 10, 11, 12, 13, 14, 15 };
int[] result = RemoveEvenNumbersLinq(array);
Console.WriteLine("Original array: [" + string.Join(", ", array) + "]");
Console.WriteLine("After removing even numbers: [" + string.Join(", ", result) + "]");
}
}
The output of the above code is
Original array: [10, 11, 12, 13, 14, 15] After removing even numbers: [11, 13, 15]
Time Complexity: O(n)
Space Complexity: O(n)
Using Recursive Approach
A recursive solution that processes the array element by element, building the result through recursive calls
using System;
using System.Collections.Generic;
class Program {
static List<int> RemoveEvenNumbersRecursive(int[] array, int index = 0) {
if (index == array.Length) {
return new List<int>();
}
List<int> rest = RemoveEvenNumbersRecursive(array, index + 1);
if (array[index] % 2 != 0) { // If current number is odd
rest.Insert(0, array[index]);
}
return rest;
}
static void Main() {
int[] array = { 2, 4, 7, 9, 12, 15 };
List<int> result = RemoveEvenNumbersRecursive(array);
Console.WriteLine("Original array: [" + string.Join(", ", array) + "]");
Console.WriteLine("After removing even numbers: [" + string.Join(", ", result) + "]");
}
}
The output of the above code is
Original array: [2, 4, 7, 9, 12, 15] After removing even numbers: [7, 9, 15]
Time Complexity: O(n²) due to Insert(0, item)
Space Complexity: O(n)
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| Iterative with List | O(n) | O(n) | High |
| LINQ Where | O(n) | O(n) | Very High |
| Recursive | O(n²) | O(n) | Medium |
Common Use Cases
- Data Filtering: Removing unwanted data points based on specific criteria in datasets.
- Mathematical Operations: Isolating odd numbers for statistical analysis or mathematical computations.
- Algorithm Preprocessing: Preparing data by filtering specific number types before applying algorithms.
Conclusion
Removing even numbers from an array in C# can be efficiently accomplished using iterative loops, LINQ methods, or recursive approaches. The LINQ Where method offers the most concise and readable solution, while the iterative approach provides better control and understanding of the filtering process.
