Performance is not the only important thing about a piece of code. Depending on the nature of the problem you might also need to consider - readability, maintainability and flexibility. I will present you couple of solutions focusing on those different aspects.
LINQ
You could use LINQ and write it like this:
private static int FirstDuplicate(IEnumerable<int> source)
{
foreach (var item in source)
{
if (source.Count(t => t == item) > 1)
{
return item;
}
}
return -1;
}
If you could've returned 0 instead of -1 in case no duplicates are found, it could've been written like this:
private static int FirstDuplicate(IEnumerable<int> source)
{
return source.FirstOrDefault(item => source.Count(t => t == item) > 1);
}
LINQ is usually quite readable and most C# programmers are familiar with it'sits syntax so it's a lot easier to just give it a glance and know what's up.
HashSet<>
The HashSet<T> is very handy when working with unique numbers as it can contain only distinct numbers. It's bool Add(T item) method will allow us to quickly check if a value was already added:
private static int FirstDuplicate(IEnumerable<int> source)
{
var distinctItems = new HashSet<int>();
foreach (var item in source)
{
if (!distinctItems.Add(item))
{
return item;
}
}
return -1;
}
Again, if you could've returned 0 instead of -1 in case no duplicates are found, it could've been written like this:
private static int FirstDuplicate<int>(IEnumerable<int> source)
{
var distinctItems = new HashSet<int>();
return source.FirstOrDefault(item => !distinctItems.Add(item));
}
Your code
It's often times easy to overthink a simple problem, such is your case, you've over-complicated a simple task. You should think through the problem, before writing code. Some people prefer to write first and than improve afterwards,afterwards; this also works, but it's always good to think a bit about it before-hand, especially. Especially when you're beginner you might often find yourself tunnel-visioned in a specific way of solving the problem once you're done with it,it; you could avoid that by thinking before-hand as you wontwon't be so attached to specific ideas.
Missing for loop indentation is also reducing the readability of your function.