Mahalasa Kini
Given an array find the First duplicate element

Given an array {1, 2, 3, 1, 5, 6,} the output should be
{1}

public void FirstRepeatingCahrecter(int[] arr){
int min=-1;
HashSet hset = new HashSet();
for(int i=arr.Length-1;i>0;i—){
if(hset.Contains(arr[i]))
min=i;
else
hset.add(arr[i]);
}
if(min!=-1){
Console.WriteLine(“The first repeating charecter is:”+ arr[min]);
}else
Console.WriteLine(“There is not repeating charecter in the array”);
}

By Mahalasa Kini in .NET on Apr 19 2020
  • Imogen Harden
    Jul, 2020 21

    var duplicates = arr.GroupBy(x => x).Select(x => new { key = x.Key, val = x.Count() });
    return duplicates.First().key;

    • 0

Most Popular Job Functions


MOST LIKED QUESTIONS