简体   繁体   中英

Find the first non-repeated character in a string using C#

I am working to solve this problem. I have to find the first nonrepeated character in a string.

For example, Input : “teeter” Output: r

I am going to first use HashTables :

class Program
{
    static void Main()
    {
        var inputString =  “teeter” ;
        Hashtable hashtable = new Hashtable();
        int numb = 1; 
        for (int i=0; i< inputString.length(); i++)
        {   
             if (!hashtable.ContainsKey(hashtable[i])) 
                 hashtable.Add(hashtable[i], numb);
             else  
                 hashtable[hashtable[i]] = hashtable[i].Value+ 1 ;
        }
    }
}

Can I solve this problem using LinQ:

numb = inputString.First(1 == inputString.Where(item => 
                              item == hashtable[i]).Count());

My questions are :

-I have to solve this problem using LINQ and using HashTables or Dictionaries. Does my solutions ture ?

// throws an ArgumentNullException if s is null.
// throws an InvalidOperationException if there is no non repeating character.
char FirstNonRepeater(string s)
{
    return s.ToLookup(c => c).First(g => g.Count() == 1).Key;
}

I don't think you need HashTable at all. Because string implements IEnumerable<char> you can use LINQ directly on your input string:

var letter = input.GroupBy(x => x).First(g => g.Count() == 1).Key;

Getting back to your HashTable solution. You 're not using it correctly. HashTable is not the same as Dictionary . It does not have key / value , it just has key s. You're looking for Dictionary here:

var inputString =  "teeter";
var dict = new Dictionary<char, int>();
int numb = 1; 
for (int i=0; i< inputString.length(); i++)
{   
     if (!dict.ContainsKey(inputString[i])) 
         dict.Add(inputString[i], numb);
     else  
         dict[inputString[i]] += 1;
}

For a solution without HashTables, Dictionaries or LINQ, just remove duplicate characters from the string:

while (s.IndexOf(s[0], 1) != -1) {
  s = s.Replace(s[0].ToString(), "");
}
char result = s[0];

You can check which characters are occured only once, and take first one out of these.

var inputString =  “teeter” ;
var output = inputString.GroupBy(x=>x).FirstOrDefault(x=>x.Count() ==1).Key;
class Program
    {
       public static void Main(string[] args)
        {
            Console.WriteLine(FirstNonRepeated("tether"));
           Console.ReadKey();
        }

        public static char? FirstNonRepeated(string word)
        {
           char[] chararray= word.ToCharArray();
            Hashtable hashtable=new Hashtable();
            foreach (var c in chararray)
            {
                if (hashtable.ContainsKey(c))
                {
                    hashtable[c]=(int)hashtable[c]+1;
                }
                else
                {
                    hashtable.Add(c,1);
                }
            }
            foreach (var v in chararray)
            {
                if ((int) hashtable[v] == 1)
                {
                    return v;
                }
            }
            return null;
        }


    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM