0

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 ?

user3001937
  • 2,005
  • 4
  • 19
  • 23
  • 2
    What is your question? – zerkms Nov 18 '13 at 01:38
  • Asking for the "best" solution is highly subjective and leads to extended discussion. Change your question to ask a specific question. Then make up your own mind, given your requirements, as to which is the better approach. – NotMe Nov 18 '13 at 01:47
  • True! I will compare the both after implementing it. – user3001937 Nov 18 '13 at 01:53
  • Does this answer your question? [Logic to get the first non-repeating(distinct) character from the string](https://stackoverflow.com/questions/3994644/logic-to-get-the-first-non-repeatingdistinct-character-from-the-string) – Herohtar Feb 16 '20 at 19:58

5 Answers5

4
// 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;
}
allonhadaya
  • 1,297
  • 7
  • 19
  • @Stanislav I think the call to `Key` would be faster since the underlying `IGrouping` will hold the char in memory instead of forcing an enumeration of the values to get the first one. – allonhadaya Nov 18 '13 at 01:52
2

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 keys. 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;
}
Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • But the question is about solving this problem using different strategies ? – user3001937 Nov 18 '13 at 01:45
  • I got inspired by this : http://msdn.microsoft.com/en-us/library/system.collections.hashtable(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-3 – user3001937 Nov 18 '13 at 01:56
  • Your program work ! Can you point me to a good article for the use of HashTables and Dictionaries in .Net ? 'cause this last one is special case of HashTables but generic – user3001937 Nov 18 '13 at 01:57
  • Yes, it's generic dictionary. I don't really think you should even consider using non-generic ones. – MarcinJuraszek Nov 18 '13 at 02:00
  • Here: Shouldn't be dict[inputString[i]].value += 1 ; as we are modefing the "Int" value ; How should access to that second attribute – user3001937 Nov 18 '13 at 02:00
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];
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
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;
Tilak
  • 30,108
  • 19
  • 83
  • 131
0
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;
        }


    }
techraf
  • 64,883
  • 27
  • 193
  • 198
Hima Susan
  • 401
  • 1
  • 4
  • 8