C# Program to Remove Even Elements from Array
Most of the IT companies check the coding skills and problem-solving skills as well along with the theoretical interview questions. Sometimes you are free to write the pseudo code and sometimes you are asked to write the complete program either on any paper or any editor.
This question is asked in the coding interview to write the program. Here you can find the program as well as a live running program so that you can test the program immediately.
Question:
How can you remove the even(or odd) elements (numbers) from an integer array? Write a program to delete the even numbers from an array.
OR
How can you remove the even index position elements from an integer array? Write a program to delete the even index position numbers from an array.
C# Program to Remove the Even Elements from an Array
// Author : Tech Point Fundamentals
// Website : www.techpointfunda.com
// Channel : https://www.youtube.com/c/TechPointFundamentals
// Demo : Removing Even Numbers from Array
using System;
using System.Linq;
public class Program
{
public static void Main()
{
int[] unsortedArray = new int[8] {5, 3, 6, 2, 1, 4, 8, 7};
Console.WriteLine("UnSorted Array With Even and Odd Elements: ");
Console.WriteLine("------------------------------------------------ ");
for( int i = 0; i < unsortedArray.Length; i++)
{
Console.WriteLine(unsortedArray[i]);
}
// Removing Even Numbers using User Defined Function
var oddArray = RemoveEvenNumbers(unsortedArray);
Console.WriteLine("\nArray After Removing Even Numbers Using UDF: ");
Console.WriteLine("------------------------------------------------ ");
for( int i = 0; i <= oddArray.Length - 1; i++)
{
Console.WriteLine(oddArray[i]);
}
// Removing Even Numbers using LINQ Library Function
var oddArrayLinq = unsortedArray.Distinct().Where( r => r % 2 != 0).ToArray();
Console.WriteLine("\nArray After Removing Even Numbers Using LINQ: ");
Console.WriteLine("------------------------------------------------ ");
for( int i = 0; i <= oddArrayLinq.Length - 1; i++)
{
Console.WriteLine(oddArrayLinq[i]);
}
}
static int [] RemoveEvenNumbers(int[] array)
{
int m = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] % 2 != 0)
{
array[m] = array[i];
++m;
}
}
int[] temp = new int[m];
for (int i = 0; i < m; i++)
temp[i] = array[i];
array = temp;
return array;
}
}
Output:
UnSorted Array With Even and Odd Elements: ------------------------------------------------ 5 3 6 2 1 4 8 7 Array After Removing Even Numbers Using UDF: ------------------------------------------------ 5 3 1 7 Array After Removing Even Numbers Using LINQ: ------------------------------------------------ 5 3 1 7
No comments:
Post a Comment
Please do not enter any HTML. JavaScript or spam link in the comment box.