Reputation: 140
My concern is adding string to array of string, but I wanted to make sure that this string is unique before inserting into the array. I searched and found many approaches for this but my concern is to make faster rather than checking all array elements for duplicate before adding the string, so I decided to do the following:
int index = 1;
int position = 0;
string s = Console.ReadLine();
byte[] ASCIIValues = Encoding.ASCII.GetBytes(s);
foreach(byte b in ASCIIValues)
{
position += b * index;
index++;
Console.WriteLine(b);
}
Upvotes: 1
Views: 3063
Reputation: 140
I used Dictionary and managed to solve it ..please check my code in below link
Hashset handling to avoid stuck in loop during iteration
and although I used proc that add two dictionary and make sure that there is no duplicate, sometime my code gives error that there was try to add a duplicate key !!!
below code I found it somewhere and works fine and in above link I used iteration to add remove during the iteration.
public static void Add2Dic(IDictionary firstDict, IDictionary secondDict, bool bReplaceIfExists)
{
foreach (object key in firstDict.Keys)
{
if (!secondDict.Contains(key))
secondDict.Add(key, firstDict[key]);
else if (bReplaceIfExists)
secondDict[key] = firstDict[key];
}
}
Upvotes: 0
Reputation: 46919
As mentioned in the comments a HashSet
would be the collection to use for this case. It represents a (unique) set of values and has O(1) lookup.
So you would just loop the strings you want to insert and add them to the set. If the string is already in there it will not be added again.
var set = new HashSet<string>();
foreach(var s in strings)
set.Add(s);
Upvotes: 8