82

i have an array of objects (Car[] for example) and there is an IsAvailable Property on the object

i want to use the full array (where IsAvailable is true for some items and false for some others) as the input and return a new array which includes only the items that have IsAvailable = true.

6 Answers 6

121

If you're using C# 3.0 or better...

using System.Linq;

public Car[] Filter(Car[] input)
{
    return input.Where(c => c.IsAvailable).ToArray();
}

And if you don't have access to LINQ (you're using an older version of .NET)...

public Car[] Filter(Car[] input)
{
    List<Car> availableCars = new List<Car>();

    foreach(Car c in input)
    {
        if(c.IsAvailable)
            availableCars.Add(c);
    }

    return availableCars.ToArray();
}
Sign up to request clarification or add additional context in comments.

7 Comments

Considering the LINQ implementation is built on IQueryable, how could its implementation be any better than the List based one?
It's not. I posted it in case the OP was using an older version of .NET. If the OP needs the most performant code, they can at least get the idea from the second example...and optimize it to suit their needs.
Nope :), still confused... I'm wondering why you are stating performance concerns on the second approach but not on the first... (just nitpicking, I think your solution is great)...
In the second example, I used a List<T> to store the elements and then output them using the .ToArray() method. I would imagine there has to be a cleaner way to do the work...and I'd hope that if one existed, LINQ to Objects would use it. I'd break out Reflector, but I'm on my Mac at the moment.
Isn't this answer lacking using System.Linq; for the first snippet?
|
51

Surprisingly, this question lacks the most natural and efficient answer: Array.FindAll

Car[] availableCars = Array.FindAll(cars, c => c.IsAvailable);

if it was a List<Car> there is also a List.FindAll.

2 Comments

I tweaked your intro. No biggie, but I hope I understood you correctly there.
not sure why, but Array.FindAll does not work with Godot C#. After using system.Linq I was able to filter using ToArray() on collection, and using .Where
6

Easiest way:

Car[] cars = //...
Car[] filtered = cars.Where(c => c.IsAvailable).ToArray();

Possibly More Efficient:

Car [] cars = //...
    List<Car> filteredList = new List<Car>();
    for(int i = 0; i < cars.Length; i++)
    {
        if(cars[i].IsAvailable)
           filteredList.Add(cars[i]);
    }
    Car[] filtered = filteredList.ToArray();

Comments

2
var available = from c in cars where c.IsAvailable == true select c;

Or

var available = cars.Where(c => c.IsAvailable == true);

1 Comment

Can leave out the explicit comparison against true, ie: var available = cars.Where(c => c.IsAvailable);
1

A simple solution is to create a new array, loop through the input array and add only those items which satisfy your conditions to the new array, and return the new array:

List<Car> available = new List<Car>();
foreach (Car c in cars) {
    if (c.IsAvailable) {
        available.add(c);
    }
}
//Here you can either just return the list, or create an array from it.

Comments

-13

an array is filter array when it meets the following conditions:

  1. if 9 exists in the list 13 must also exist
  2. if 7 exists in the list then 11 must not exist

solution

int[] a = {7 , 72, 6, 13, 9 };
int i, k = 0, l = 0, m = 0, n = 0;
for (i = 0; i < a.Length; i++)
{
    if (a[i] == 9)
    {
        k = 1;
    }
}
for (i = 0; i < a.Length; i++)
{
    if (a[i] == 13)
    {
        l = 1;
    }
}
for (i = 0; i < a.Length; i++)
{
    if (a[i] == 7)
    {
        m = 1;
    }
}
for (i = 0; i < a.Length; i++)
{
    if (a[i] == 11)
    {
        n= 1;
    }
}
if ((k == 1 && l == 1) && (m == 1 && n == 1))
{
    Console.WriteLine("is not filter array");
}
else if (k == 1 && l!= 1)
{
    Console.WriteLine("is not filter array");
}
else if (m ==1 && n==1)
{
    Console.WriteLine("is not filter array ");
}
else
    Console.WriteLine("is filter array");
Console.WriteLine("the element of an array is:");
for (i = 0; i < a.Length; i++)
{
    Console.WriteLine(a[i]);
}

As i think this code is surely work for if you need to test an array.
reta seboka ambo universtity woliso campuse department of information TECH.!!

1 Comment

This is not filtering an array. This is returning whether certain values are in the array, in a very bad way at that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.