
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Below are different approaches for removing all odd numbers from an array
- Using Iterative Approach
- Using LINQ
- Using Recursive Approach
Removing Odd Numbers Using Iterative Approach
This is a simple and direct approach to removing odd numbers from an array. We use an iterative approach to remove all odd numbers from an array. In this method, we traverse the array and check if each element is odd or even. If the number is even, it is added to a new list. Convert the list back to an array.
Implementation Code
using System; using System.Collections.Generic; class Program { static int[] RemoveOddNumbersIterative(int[] array) { List<int> result = new List<int>(); // Specify List<int> for storing integers 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)); } }
Output
The array after removing all odd elements is: 2, 4, 6
Time Complexity: O(n)
Space Complexity: O(n)
Removing Odd Numbers Using LINQ
In this method, we use the 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.
Implementation Code
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)); } }
Output:
The array after removing all odd elements is: 14, 18, 20
Time Complexity: O(n)
Space Complexity: O(n)
Removing Odd Numbers Using Recursive Approach
In this approach, we use recursion to remove odd numbers from an array. In this approach, we check each element and construct a new array containing only even numbers through recursive calls.
Implementation Code
using System; using System.Linq; class Program { static int[] RemoveOddNumbersRecursive(int[] array) { // Base case: empty array if (array.Length == 0) return Array.Empty<int>(); // Specify the type int here // 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, 9, 13, 15 }; int[] result = RemoveOddNumbersRecursive(array); Console.WriteLine("The array after removing all odd elements is: {0}", string.Join(", ", result)); } }
Output:
The array after removing all odd elements is:
Time Complexity: O(n)
Space Complexity: O(n)
Real-Life Applications
-
Data Cleaning:
Sometimes, it is needed to remove odd or unnecessary data values from datasets. -
Filtering Operations:
It is used to filter specific types of data, such as even numbers in sequences. -
Optimization:
It is used in reducing unnecessary values in arrays for mathematical or computational operations.