Find first repeated value; print it or -1.
| Test | Status | Details |
|---|
Ready — edit the code above and click Run or Submit.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
int[] a = { 1, 2, 3, 2, 4 };
var seen = new HashSet<int>();
foreach (var x in a) {
if (!seen.Add(x)) { Console.WriteLine(x); return; }
}
Console.WriteLine(-1);
}
}
Try solving on your own first, then reveal the official answer.
HashSet detects duplicate on insert.