I am currently programming in c and I was wondering if there's a smart way of filtering an array to make another array satisfying certain conditions.
An example problem would be: Given an array of random integers of size 10, generate an array containing only even numbers that were in the array.
Since it is difficult to know how many elements satisfy the condition, I am checking through the array twice, once counting the number of elements that satisfy the condition, and then actually putting the corresponding elements into the array.
One other thing I tried is making an integer array of size 10, storing all indices satisfying the conditions on the first run and then just reading off the elements in the array of the desired index when copying desired elements into the array.
In general, the array may be huge and checking conditions may be expensive, so I don't think this method would do well.
I feel like there should be smarter & more efficient ways to do this. Could you help me out?