- Home /
How to exclude elements from int[]
I'm using Mathf.Min within int[] variable.
Is there a way to find minimum, but more than 0?
And second question: How to exclude all elements that are "0" from my array (or whatever it's called when using int[]).
I found some statements like removeAt or push, but those don't work with int[].
Thank you.
Answer by Eudaimonium · Jul 18, 2015 at 05:33 PM
Arrays are bit tricky. Any particular reason why are you using integer array, instead of Integer Lists?
If you create,
List<int> myList = new List<int>();
then you can do: myList.Minimum, Sort, Reverse, RemoveAt, InsertAt, and tons of other cool functions.
If you insist on using int array, then you just gotta do it all by hand.
First use one For loop to count all zeroes in your array. Create another int array that is the size of original array minus zero counts.
Second for loop that copies over original array to new array, except when element is zero.
int[] original = //assumed
int[] newArray;
int zeroCounter;
int minimumInArray = int.maxValue;
for(int i = 0; i<original.Length; i++)
{
if (original[i] > 0 && original[i] < minimumInArray)
minimumInArray = original[i];
if (original[i] == 0)
zeroCounter++;
}
newArray = new int[original.Length - zeroCounter];
int index = 0;
for(int i = 0; i<original.Length; i++)
{
if (original[i] != 0)
newArray[index++] = original[i];
}
//At this point:
//minimumInArray contains the lowest value from original array that is not zero
//newArray contains all values from original except zero elements.
Nice, thanks! I missed that zeroCounter part :)
As for my particular reason: I have too many intersected scripts using int[], that it's way too complex to switch everything back to Lists. But I'll probably remake it later.
I'd like to mention that System.Linq
has a lot of useful list functions too. For future reference, the operation of excluding certain elements form a list is commonly known as "filter," and the operation of combining all the elements in a list (finding $$anonymous$$, max, sum, etc.) is commonly known as "reduce." Here, they're renamed to "where" and "aggregate," respectively.
Here is an example of using Where
to filter a list: https://msdn.microsoft.com/en-us/library/vstudio/bb549418(v=vs.100).aspx
From that page, it seems that Linq
does work with arrays, so put using System.Linq; at the top of your script and you can do something like this:
int[] myIntArray = /* Some numbers */;
int[] myIntArrayPositive = myIntArray.Where((number, index) => number > 0);
int $$anonymous$$imumPositive = myIntArrayPositive.Aggregate((numA, numB) => $$anonymous$$athf.$$anonymous$$in(numA, numB));
Your answer
Follow this Question
Related Questions
JS: Array index not taking array.length as valid int? 2 Answers
Checking to see if a loaded level is equal to a member of an integer array 1 Answer
get int based on 3 ints 0 Answers
Extracting Numbers from String Place JS 1 Answer
How do you calculate walls between tiles in a 3d environment? 5 Answers