envyM6
envyM6

Reputation: 1237

How to loop to remove all instance of an value from same array?

I have two different arrays for my form as follows:

int[] X = { 14, 19, 24, 28, 33 };
int[] Y = { 90, 131, 132, 150, 170 };

And I can remove one value from both array (same index number) using:

 int maxValue = Y[0];
 for (int i = 1; i < Y.Length; i++)
 {
      if (Y[i] > maxValue)
      maxValue = Y[i]; // max value in the array.

 }

 if (maxValue < 100)                                    
 {
     int indexY = IndexOfInt(Y, maxValue);
     Y = Y.Except(new int[] { maxValue }).ToArray();
     List<int> Z = X.ToList();
     Z.RemoveAt(indexY);
     X = Z.ToArray();
 }

static int IndexOfInt(int[] arr, int value) //finds the index number  
        {
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == value)
                {
                    return i;
                }
            }
            return -1;

        }

The code above returns:

int[] X = { 19, 24, 28, 33 };
int[] Y = { 131, 132, 150, 170 };

How do I go about and remove multiple instances of the same value, lets say

int[] X = { 14, 19, 24, 28, 33 };
int[] Y = { 90, 90, 132, 150, 170 };

I'm guessing it has to be a while loop but just can't get the loop or the concept right. It's not about avoiding duplicates. Its about removing all the values that is <100. I want to get rid of values that is less than 100 from X array from, for example:

int[] Y = { 90, 90, 132, 150, 170 };

should be

int[] Y = { 132, 150, 170 };

And because we are removing X[0] X[1] we have to remove Y[0] Y[1]

int[] X = { 24, 28, 33 };

As I have to use this array to create an Chart in my form. I Hope this clear things up for Why.

Upvotes: 1

Views: 187

Answers (2)

Grant Winney
Grant Winney

Reputation: 66439

I'm trying to remove the instances of values which lower than 100 in Y and corresponding values which has same index number in X. i.e my code removes Y[0] and X[0]

The following code gets the indexes of all elements in Y that are less than 100 (minus those indexes that don't exist in X, assuming it's possible for X to be shorter than Y), then removes the elements at all of those indexed positions from both X and Y:

var X = new List<int> { 14, 19, 24, 28, 33 };
var Y = new List<int> { 90, 90, 132, 150, 170 };

var indexes = Y.Select((element, index) => new {element, index})
               .Where(item => item.element < 100
                              && item.index < X.Count)
               .Select(item => item.index)
               .OrderByDescending(i => i)
               .ToList();

foreach (var index in indexes)
{
    X.RemoveAt(index);
    Y.RemoveAt(index);
}

The collections will end up with { 24, 28, 33 } and { 132, 150, 170 } in them.

Upvotes: 2

Edmund Schweppe
Edmund Schweppe

Reputation: 5132

How about the Distinct<TSource> method?

int[] Y = { 90, 90, 132, 150, 170 };
Y = Y.Distinct().ToArray();
Console.Write("Distinct values: ");
foreach(int d in Y)
{
    Console.Write("{0} ",d);
}
Console.WriteLine();

Resulting output: Distinct values: 90 132 150 170

Upvotes: 2

Related Questions