0

I got an array

string [] strings = new string[] {"1", "2", "2", "2", "1"};

You can see the value of the array is just 1 and 2, just 2 values, you could say, and I want to get those value out...What I did is here, just a start:

string[] strings = new[] { "1", "2", "2", "2", "1"};
int[] ints = strings.Select(x => int.Parse(x)).ToArray();

I don't what is next...Anyone helps?

1
  • 1
    I don't what is next? What do you want to do with the extracted int[]? Commented May 13, 2015 at 14:58

3 Answers 3

4

You mean you just want an array int[] {1, 2}?

string[] strings = new[] { "1", "2", "2", "2", "1"};
int[] ints = strings.Select(int.Parse).Distinct().ToArray();
Sign up to request clarification or add additional context in comments.

Comments

2

You may simply add a distinct to get the unique values:

int[] ints = strings.Select(x => int.Parse(x)).Distinct().ToArray();

Thus your array contains the elements {1, 2}

1 Comment

Nice,,Thanks a lot~~~
1

Classic way:

        string[] strings = new[] { "1", "2", "2", "2", "1" };
        List<int> items = new List<int>();

        for (int i = 0; i < strings.Length; i++)
        {
            int item = int.Parse(strings[i]);

            if (!items.Contains(item))
                items.Add(item);
        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.