Odd Occurrence Array I am trying to use a dictionary in Odd Occurrence. Any suggestions on how I could optimize the program? Codility is giving me a result of 66%.

var dictionary = new Dictionary&lt;int, short&gt;();
int oddVal = 0; //Collect the number with Odd Occurrence

//Add the numbers in the dictionary with value 1-odd pair or 2-pair
if(A.Length != 0)
{
    for (var i = 0; i &lt; A.Length; i++)
    {
        if (dictionary.ContainsKey(A[i]))
            dictionary[A[i]] = 2;
        else
            dictionary.Add(A[i], 1);

    }
    //review with the dictionary entry to see which key is the odd occurrence 1
    //Once odd occurrence is determined then break out of the loop
    foreach (KeyValuePair&lt;int, short&gt; entry in dictionary)
    {
        if (entry.Value == 1)
        {
            oddVal = entry.Key;
            break;
        }
    }

    return (int)oddVal;
}

else
    return 0;
For one I don't believe your code will cover all the possible values.  If the values passed in are 9,3,9,3,9,7,7 I believe they would want 9 to be the result since there is an odd number of 9.  Currently you are checking for a single value.  

By using a dictionary you first must add all the values to it then loop over it again.  Another option would be to check if the key exist.  If not then add it but if it does then remove it.  Then all that would be left if the "odd occurrences".  If we do it that way we don't even need to have the value part of the dictionary just the key part.  If we just need the key part of the dictionary then we should use a HashSet.   

so the code would look something like this (I didn't run this code just typed in here)

foreach (var item in A)
{
   if (!hashset.Add(item))
   {
       hashset.Remove(item);
   }
}

return hashset.Single();


I never used codility so I don't know what they would score this solution.

Also as a side note there is no reason to cast the oddval in your code as an int in the return statement.  It's already declared as an int.