LeetCode: Intersection of two arrays ii C#
if (!nums1Count.ContainsKey(num))
            {
                nums1Count.Add(num,1);
            }
            else
            {
                nums1Count[num]++;
            }



I think it can be written as:

nums1Count.TryGetValue(num, out int count);
nums1Count[num] = count + 1;


You don't have to test the return value of TryGetValue() in this case, because TryGetValue() sets count to default (0) if it returns false.

The loop could also be written as a LINQ sequence as:

var nums1Count = data.GroupBy(i =&gt; i).ToDictionary(gr =&gt; gr.Key, gr =&gt; gr.Count());




Likewise:


            if (nums1Count.ContainsKey(num) &amp;&amp; nums1Count[num] &gt; 0)
            {
                res.Add(num);
                nums1Count[num]--;
            }



can be changed to:

if (nums1Count.TryGetValue(num, out int count) &amp;&amp; count &gt; 0)
{
  res.Add(num);
  nums1Count[num]--;
}