Find duplicate Numbers repeated more than once C#

Group your items and take only those with more than 2 occurrences:

array.GroupBy(x=>x).Where(x=>x.Count()>2).Select(x=>x.Key)

Since there aren't any constraints provided to the elements that can be contained in this array, you should have asked the interviewer whether he wants a solution with O(n) time complexity and O(n) space complexity** or a solution with O(nlogn) time complexity and O(1) space complexity**.

Without constraints to the elements in the array there's no solution in O(n) time complexity and O(1) space complexity**.

And because he rejected your solution (which is O(n) time complexity and O(n) space complexity**) apparently he was seeking for the second. One way to achieve that is to first sort the array and then iterate over it to find the duplicates.

Remark**: the example values provided for space complexity do not include the space occupied by the original array, only the extra space needed.

Tags:

C#

Arrays