|
Hi Brij,
"some generic for that" -- do you have some more words on this? Or some pseudo code?
regards,
George
|
|
|
|
|
List<string> newArray = new List<string>();
foreach (string token in yourArray)
{
if (!newArray.Contains(token))
{
newArray.Add(token);
}
}
|
|
|
|
|
Thanks Igor,
I like your solution!
regards,
George
|
|
|
|
|
no worries
|
|
|
|
|
Make a custom function,In which create an a generic as taken below a list.
List<string> UnqueList=new List<string>();
for (int i = 0; i < strarr.Length; i++)
{
if(!UnqueList.Exists(strarr[0]))
{
UnqueList.Add(strarr[0]);
}
}
Now you'll the list conatining unique elements.You can conert it to array too as
UnqueList.ToArray();
Cheers!!
Brij
|
|
|
|
|
Thanks Brij!
The "generic" you mean List<string>?
regards,
George
|
|
|
|
|
Generic does means only List.We have some more like Dictionary,SortedList,Queue,Stack but list suits your requirement best.
Cheers!!
Brij
|
|
|
|
|
Thanks for your clarification, Brij!
regards,
George
|
|
|
|
|
If I'm not mistaken you can use LINQ to select unique values.
|
|
|
|
|
...yes, assuming he's using .net 3+
|
|
|
|
|
Yes, I have to use .Net 3.0, not .Net 3.5. Any ideas for .Net 3.0 based solution? I think LINQ belongs to .Net 3.5, not .Net 3.0?
regards,
George
|
|
|
|
|
LINQ is good, but I have to use .Net 3.0, not .Net 3.5. Any ideas for .Net 3.0 based solution?
regards,
George
|
|
|
|
|
You can use the Distinct method(an Extention Method) if you are using C#3.0 and the implementation code is quit simple, such as:
string[] strs = new string[] { "abc", "bcd", "abc" };
IEnumerable<string> newStrs = strs.Distinct();
Hope this will help.
LuckyBoy
|
|
|
|
|
LuckyBoy,
Distinct belongs to .Net 3.5, and I have to use .Net 3.0.
Any ideas for .Net 3.0?
regards,
George
|
|
|
|
|
George_George wrote: I have to use .Net 3.0
Then I can't suggest HashSet.
But I can suggest my Set class.
|
|
|
|
|
What are the advantages of your Set class over .Net Set class?
regards,
George
|
|
|
|
|
|
Could you show more description please? What operators do you think your class provides has advantages over .Net built-in ones?
regards,
George
|
|
|
|
|
Not sure why people are saying there isn't a built in set class. Use Hashset< string>.
Insertion and checking for existing values is roughly O(n). Has extension methods on it for doing linqy kind of things.
Also noticed a lot of people said "use linq!". Linq does not make things run faster - it's not a magic replacement for Array.Find. It just makes your code look pretty, thats all
|
|
|
|
|
Thanks Mark,
I think people means no built-in single call for find the uniqueness for string.
BTW: if LINQ is slow, why people will use LINQ?
regards,
George
|
|
|
|
|
*shrug* I think Hashset< T>.Add(T item) returning bool if it was unique is close enough.
People use LINQ because it makes the code more readable. Generally CPU is cheap and good programmers aren't. Its ok to have a 10% overhead if your code is more reliable and easier to maintain as a result.
|
|
|
|
|
Mark Churchill wrote: Generally CPU is cheap and good programmers aren't
That's a good one
|
|
|
|
|
Thanks Mark,
1. "*shrug* I think Hashset< T>.Add(T item) returning bool if it was unique is close enough." -- I am still confused why do you think .Net built-in Hashset is good enough. Any comments?
2. I know about LINQ but not very experienced. My concern about LINQ is, I do not think it is more readable, why do you think it is more readable?
3. LINQ is only 10% slower? I think it is much slower in my experience. Do you have any performance benchmarking data?
regards,
George
|
|
|
|
|
George_George wrote: BTW: if LINQ is slow, why people will use LINQ?
I think Mark is right. We can get the benefits from LINQ for integrating the data and object. But I am not sure about the performance of LINQ. Though I do believe Mircosoft would make greate efforts to improve it.
LuckyBoy
|
|
|
|
|
In my experience and other guys besides me, the performnace feedback of LINQ is bad.
What about yours?
regards,
George
|
|
|
|