Remove All Even 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 need to remove all the even numbers from it. The resulting array should only contain odd numbers. In this article, we are going to discuss different approaches to solving this problem using C#.

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.

Below are different approaches for removing all even numbers from an array:

Removing Even Numbers Using Iterative Approach

This is a simple and direct approach for removing even numbers from an array. We use an iterative approach to remove all even numbers. In this method, we traverse the array and check if each element is odd or even. If the number is odd, it is added to a new list. The list is then converted back to an array.

Implementation Code

using System;
using System.Linq;

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

    static void Main()
    {
        int[] array = { 1, 2, 3, 4, 5, 6 };
        int[] result = RemoveEvenNumbers(array);
        // Output the custom message with the result
        Console.WriteLine("The array after removing all even elements: " + string.Join(", ", result));
    }
}

Output:

The array after removing all even elements is: 1, 3, 5
Time Complexity: O(n)
Space Complexity: O(n)

Removing Even Numbers Using LINQ

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

Implementation Code

using System;
using System.Linq;

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

  static void Main() {
    int[] array = {
      10,
      11,
      12,
      13,
      14,
      15
    };
    int[] result = RemoveEvenNumbersLinq(array);
    // Output the result with the correct message
    Console.WriteLine("The array after removing all even elements is: {0}", string.Join(", ", result));
  }
}

Output:

The array after removing all even elements is: 11, 13, 15

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

Removing Even Numbers Using Recursive Approach

In this approach, we use recursion to remove even numbers from an array. This method checks each element and constructs a new array containing only odd numbers through recursive calls.

Implementation Code

using System;
using System.Collections.Generic;

class Program {
  static List<int> RemoveEvenNumbersRecursive(int[] array, int index = 0) {
    // Base case: when index reaches the end of the array
    if (index == array.Length) return new List<int>();

    // Recursive call
    var rest = RemoveEvenNumbersRecursive(array, index + 1);
    if (array[index] % 2 != 0) {
      rest.Insert(0, array[index]); // Insert the odd number at the front
    }
    return rest;
  }

  static void Main() {
    int[] array = { 2, 4, 7, 9, 12, 15 };
    List<int> result = RemoveEvenNumbersRecursive(array);
    Console.WriteLine("The array after removing all even elements is: {0}", string.Join(", ", result));
  }
}

Output

The array after removing all even elements is: 7, 9, 15

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

Real-Life Applications

  • Data Cleaning:
    It is useful for removing even or unnecessary data values from datasets.
  • Filtering Operations:
    It is used to filter specific types of data, such as odd numbers in sequences.
  • Optimization:
    It can reduce unnecessary values in arrays for mathematical or computational operations.
Updated on: 2025-01-24T16:21:11+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements