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?

Eleanor
  • 569
  • 1
  • 8
  • 17

3 Answers3

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();
Charles Mager
  • 25,735
  • 2
  • 35
  • 45
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}

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
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);
        }