tags:

views:

30

answers:

1

I have a text file which contains lines of string for example:

a, b, c, d, e, d
t, e, rt, q, r, e, t, w, d, t

I need to make sure that in each line I read from a file, no repeater character or string is allowed.I need to check and make sure there is no repeater character in each line.So how would I check that?

I'm thinking of creating 2d array and check it but then in each line has unlimited length.Is there any other way of checking it?

+2  A: 

Use a HashSet for each line to store the already read values.

The algorithm would be something like this (roughly)

HashSet<string> hashSet = new HashSet<string>();
bool hasDuplicate = false;
string[] lineEntries = line.split(", ");
foreach (string s in lineEntries)
{
   if (hashSet.contains(s))
      hasDuplicate = true;
   hashSet.add(s);
}

That's the code for one line only. You could expand this for all the lines.

Samuel Carrijo
Can you give me example please?
gingergeek
added an example. Post another comment if you need more help
Samuel Carrijo
Thanks a lot this is helpful.
gingergeek