Searching item in array I have a program that calculates some values and saves them to an array. Before save a value, program checks if there is already an identical value in array:

 string someData = GetData();
 bool newItem = true;
 int arrayDataLength = arrayData.Length;
 for (int x = 0; x &lt; arrayDataLength; x++)
 {
     if (arrayData[x] == someData)
     {
         newItem = false;
         arrayDataCount[x]++;
         break;
     }
  }
  if (newItem == true)
  {
       Array.Resize(ref arrayData, (arrayDataLength + 1));
       Array.Resize(ref arrayDataCount, (arrayDataLength + 1));
       arrayData[arrayDataLength] = someData;
       arrayDataCount[arrayDataLength]++;
  }


Sometimes arrays with data become big, and program works a bit slow. 
How can I optimize perfomance of this action?
If you don't need to count the number of occurences, use a Set:

private readonly ISet&lt;string&gt; data = new HashSet&lt;string&gt;();

public void AddNewData()
{
    string someData = GetData();
    data.Add(someData);
}




If you need to count the occurences, you should use a Dictionary:

private readonly IDictionary&lt;string, int&gt; data = new Dictionary&lt;string, int&gt;();

public void AddNewData()
{
    string someData = GetData();
    if (data.ContainsKey(someData))
    {
        data[someData]++;
    }
    else
    {
        data[someData] = 1;
    }
}




Alternatively, you could just use a List and calculate the counts when you need them, using the LINQ GroupBy extension method:

private readonly ICollection&lt;string&gt; data = new List&lt;string&gt;();

public void AddNewData()
{
    string someData = GetData();
    data.Add(someData); // data might contain duplicates
}

public void PrintData() // use LINQ to count number of occurences
{
    foreach (var entry in data.GroupBy(x =&gt; x))
    {
        Console.WriteLine("{0} {1}", entry.Key, entry.Count());
    }
}