C# Sharp Exercises: Remove all the values except integer values from a given array of mixed values
C# Sharp Basic: Exercise-91 with Solution
Write a C# Sharp program to remove all the values except integer values from a given array of mixed values.
Sample Solution:
C# Sharp Code:
using System;
using System.Linq;
namespace exercises
{
class Program
{
static void Main(string[] args)
{
object[] mixedArray = new object[6];
mixedArray[0] = 25;
mixedArray[1] = "Anna";
mixedArray[2] = false;
mixedArray[3] = System.DateTime.Now;
mixedArray[4] = -112;
mixedArray[5] = -34.67;
Console.WriteLine("Original array elements:");
for (int i = 0; i < mixedArray.Length; i++)
{
Console.Write(mixedArray[i]+" ");
}
int[] new_nums = test(mixedArray);
Console.WriteLine("\n\nAfter removing all the values except integer values from the said array of mixed values:");
for (int i = 0; i < new_nums.Length; i++)
{
Console.Write(new_nums[i]+" ");
}
}
public static int[] test(object[] nums)
{
return nums.OfType<int>().ToArray();
}
}
}
Sample Output:
Original array elements: 25 Anna False 4/24/2021 11:43:11 AM -112 -34.67 After removing all the values except integer values from the said array of mixed values: 25 -112
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to count number of ones and zeros in the binary representation of a given integer.
Next: Write a C# Sharp program to find the next prime number of a given number. If the given number is a prime number, return the number.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework