My solution for the leet code problem to search an array of ints for duplicate values and return a boolean seems quite efficient (< 90% of submissions runtime). However, I am currently reviewing Data Structures and Algorithms and I wonder if there is a more efficient approach to this, since if my counting is correct my solution will run at O(n) for worst case scenarios. I am still new to C# (I mostly code in js).
public bool ContainsDuplicate(int[] nums) {
HashSet<int> singles = new HashSet<int>();
for(int i = 0; i < nums.Length;i++)
{
if (singles.Contains(nums[i]))
return true;
singles.Add(nums[i]);
}
return false;
}
O(n)? because theContainshas an iterator as well which puts itO(n^2)\$\endgroup\$