From my understanding, a simple for loop will have a complexity of O(n).
foreach(var record in records) {
// ...
}
If I introduce a Hash lookup within the foreach, will that keep the complexity to O(n)?
var ids = new HashSet<int>();
foreach(var record in records) {
bool isRecordInIdSet = ids.Contains(record.id);
}
Likewise, if the HashSet was instead a list, would that make the complexity to O(n^2)?
var ids = new List<int>();
foreach(var record in records) {
bool isRecordInIdSet = ids.Contains(record.id);
}
I was gleaning from this question What is the lookup time complexity of HashSet<T>(IEqualityComparer<T>)? , and would love to make sure my understanding is correct. Please add any additional info that you think would be useful to understanding Big-O terminology.