Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to get the indices of elements of an array with particular element. Is it okay or, or how can I make it even better?

//string Input = "Net Sales,COGS";

int[] numbers = { 0, 1, 2, 3, 4, 5 };
string[] Values = { "Net Sales", "COGS", "COGS%", "Gross Profit", "GP%", "Quantity" };

string[] selectedValues = Input.Split(new char[] { ',' });
selectedIndex = new int[selectedValues.Length];
selectedIndex = selectedValues.Select(n => Array.IndexOf(Values, n)).ToArray();

for (int i = 0; i < selectedIndex.Length; i++)
    numbers = numbers.Where(val => val != selectedIndex[i]).ToArray();

selectedIndex = numbers;
share|improve this question
up vote 5 down vote accepted

First of all your code doesn't compile.

Second, your code doesn't do what you state - "Remove particular element from an array".

Third, what exactly do you mean by "Remove"? You want to set these elements to null or compact your array? In second case what do you need indexes of other elements for (their indexes will change when you compact your array)?

Ok, now lets start with a declaration of a contract:

You want to get a multivalue result: filtered array and indexes of remaining elements.

public struct Result
{
    public readonly string[] FilteredArray;
    public readonly List<int> IndexesOfRemainingElements;

    public Result(string[] filteredArray, List<int> indexesOfRemainingElements)
    {
        FilteredArray = filteredArray;
        IndexesOfRemainingElements = indexesOfRemainingElements;
    }
}

Now, I'd rewrite your method in this way:

static Result FilterArray(string[] array, string[] input)
{
    var remainingElements = array.Where(i => !input.Contains(i)).ToArray();

    return new Result(remainingElements, 
                      remainingElements.Select(i => Array.IndexOf(array, i)).ToList());
}

And finally you can call it:

string input = "Net Sales,COGS";
string[] values = { "Net Sales", "COGS", "COGS%", "Gross Profit", "GP%", "Quantity" };

// Split has an override that accepts params char[]
var result = FilterArray(values, input.Split(','));    

Complete example here: https://dotnetfiddle.net/4wbfpC

share|improve this answer
1  
Semantically it would be clearer to use Except to calculate the remainingElements: var remainingElements = array.Except(input).ToArray() – ChrisWue Mar 10 '15 at 8:33
1  
@ChrisWue, I usually try to be exceptionally cautious with set-based methods like Except because they also apply Distinct (stackoverflow.com/questions/2975944/…). – aush Mar 10 '15 at 8:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.