4
Reply

Given an array find the First duplicate element

Mahalasa Kini

Mahalasa Kini

5y
2.1k
5
Reply

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”);
}

    1. var duplicates = arr.GroupBy(x => x).Select(x => x.Count() > 1);
    2. return duplicates.First().key;

    string[] qwerty = { β€œZ”, β€œA”, β€œC”, β€œH”, β€œF”, β€œV”, β€œF”, β€œH”, β€œA” };

    HashSet NoRepeat = new ();
    string? first_repeat = null;
    foreach (var item in qwerty)
    {
    if(!NoRepeat.Add(item))
    {
    first_repeat = item;
    break;
    }
    }
    Console.WriteLine(first_repeat);

    var duplicates = arr.GroupBy(x => x).Where(x => x.Count() });
    return duplicates.First().key;

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