Easy to understand C#


  • 0
    public class Solution {
        public int SingleNumber(int[] nums) {
            
            Dictionary<int, int> results = new Dictionary<int, int>();
            for(int i = 0;i<nums.Length;i++)
            {
                if(results.ContainsKey(nums[i]))
                {
                    results[nums[i]] +=1;
                }
                else
                {
                    results.Add(nums[i], 1);
                }
            }
            
            
            foreach(KeyValuePair<int, int> kvp in results)
            {
                if(kvp.Value == 1)
                {
                    return kvp.Key;
                }
            }
            return -1;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.