LeetCode: Sliding Window Median C#
Your ID is a long created from 2 ints, so you can fit both ints inside this one long side by side:

long key = (long)nums[i] &lt;&lt; 32 | i;




You have a lot of unnecessary if statement evaluations. You could get rid of them by adding the first k elements from the array into the list in a separate loop, and then starting a second loop from i = k that can safely do med.Remove and med.Add without ifs.

k never changes, so instead of evaluating k % 2 == 0 and k / 2 and k / 2 - 1 in every iteration, you can store the final value in a variable before the loop begins. Then in the loop you only need to call

res.Add(med[med.Keys[medianIndex]]);


or

res.Add( ((long)med[med.Keys[medianIndex]] + med[med.Keys[medianIndex2]])/2.0 );




Making res a list and in the end converting it into an array slows your code down a lot. You can make it an array (of size nums.Length - k + 1). Lists come with the overhead of their extra functionality, so you waste time not only on the conversion to an array but also on every time you add a number to the list.