How to find the first character that is repeated in a string with C#

1 Answer

0 votes
using System;

class Program
{
    static int get_first_character_repeated(string s) { 
        int len = s.Length;
          
        for (int i = 0; i < len; i++) { 
            for (int j = i + 1; j < len; j++) { 
                if (s[i] == s[j]) { 
                    return i;
                } 
            } 
        } 
        return -1; 
    }  
    static void Main() {
        string s = "abcdxypcbop"; 
       
        int i = get_first_character_repeated(s); 
      
        if (i == -1) 
            Console.WriteLine("Not found"); 
        else
            Console.WriteLine(s[i]); 
    }
}



/*
run:

b

*/

 



answered Jan 25, 2020 by avibootz
edited Jan 25, 2020 by avibootz

Related questions

...