Remove all odd numbers from an array in C#

Natural numbers are a set of positive integers, which are commonly used for counting and ordering. They begin from 1 and continue infinitely. In some contexts, it may also include 0. Here, we are going to learn about different approaches to removing all odd numbers from a given array using C#.

Problem Description

We are given an array, and we need to remove all the odd numbers from it. The resulting array should only contain even numbers.

Example 1

  • Input: array = {1, 2, 3, 4, 5, 6}
  • Output: {2, 4, 6}

Explanation:

The odd numbers (1, 3, 5) are removed, leaving only the even numbers.

Example 2

  • Input: array = {11, 14, 18, 21, 29}
  • Output: {14, 18}

Explanation:

The odd numbers (11, 21, 29) are removed, leaving only the even numbers.

Removing Odd Numbers from Array Original Array: 1 2 3 4 5 6 Remove Odd Numbers Result Array: 2 4 6

Below are different approaches for removing all odd numbers from an array

  • Using Iterative Approach
  • Using LINQ
  • Using Recursive Approach

Using Iterative Approach

This is a simple and direct approach to removing odd numbers from an array. We traverse the array and check if each element is odd or even using the modulo operator (%). If the number is even, it is added to a new list. Finally, we convert the list back to an array.

Example

using System;
using System.Collections.Generic;

class Program {
   static int[] RemoveOddNumbersIterative(int[] array) {
      List<int> result = new List<int>();
      foreach (var num in array) {
         // Check if the number is even
         if (num % 2 == 0) {
            result.Add(num);
         }
      }
      return result.ToArray();
   }

   static void Main() {
      int[] array = { 1, 2, 3, 4, 5, 6 };
      int[] result = RemoveOddNumbersIterative(array);
      Console.WriteLine("The array after removing all odd elements is: " + string.Join(", ", result));
   }
}

The output of the above code is

The array after removing all odd elements is: 2, 4, 6

Time Complexity: O(n)
Space Complexity: O(n)

Using LINQ

In this method, we use LINQ to filter out all odd numbers from the array. Using the Where method, we can create a new array that contains only even numbers in a single line of code.

Example

using System;
using System.Linq;

class Program {
   static int[] RemoveOddNumbersLinq(int[] array) {
      // Use LINQ to filter even numbers
      return array.Where(number => number % 2 == 0).ToArray();
   }

   static void Main() {
      int[] array = { 11, 14, 17, 18, 20 };
      int[] result = RemoveOddNumbersLinq(array);
      Console.WriteLine("The array after removing all odd elements is: {0}", string.Join(", ", result));
   }
}

The output of the above code is

The array after removing all odd elements is: 14, 18, 20

Time Complexity: O(n)
Space Complexity: O(n)

Using Recursive Approach

In this approach, we use recursion to remove odd numbers from an array. We check each element and construct a new array containing only even numbers through recursive calls.

Example

using System;
using System.Linq;

class Program {
   static int[] RemoveOddNumbersRecursive(int[] array) {
      // Base case: empty array
      if (array.Length == 0) return new int[0];

      // Check the first element and process recursively
      int[] rest = RemoveOddNumbersRecursive(array.Skip(1).ToArray());
      if (array[0] % 2 == 0) {
         return new int[] { array[0] }.Concat(rest).ToArray();
      }
      return rest;
   }

   static void Main() {
      int[] array = { 7, 8, 9, 10, 13, 16 };
      int[] result = RemoveOddNumbersRecursive(array);
      Console.WriteLine("The array after removing all odd elements is: {0}", string.Join(", ", result));
   }
}

The output of the above code is

The array after removing all odd elements is: 8, 10, 16

Time Complexity: O(n²)
Space Complexity: O(n)

Comparison of Approaches

Approach Time Complexity Space Complexity Readability
Iterative O(n) O(n) High
LINQ O(n) O(n) Excellent
Recursive O(n²) O(n) Moderate

Real-Life Applications

  • Data Cleaning:
    Filtering out unwanted data values from datasets based on specific criteria.
  • Filtering Operations:
    Used to filter specific types of data, such as even numbers in sequences for mathematical calculations.
  • Optimization:
    Reducing unnecessary values in arrays for computational operations or memory efficiency.

Conclusion

Removing odd numbers from an array can be accomplished using iterative, LINQ, or recursive approaches. The LINQ method offers the most concise and readable solution, while the iterative approach provides good performance and clarity. Choose the method that best fits your project requirements and coding style preferences.

Updated on: 2026-03-17T07:04:36+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements