Find unique strings for a string array
-
Hello everyone, I have a string array, but may have duplicate strings. Any built-in or smart way to remove the duplicate ones and generate a string array contains only unique ones? For example, the input array is {"abc", "bcd", "abc"}, the unique output array is {"abc", "bcd"}. thanks in advance, George
-
Hello everyone, I have a string array, but may have duplicate strings. Any built-in or smart way to remove the duplicate ones and generate a string array contains only unique ones? For example, the input array is {"abc", "bcd", "abc"}, the unique output array is {"abc", "bcd"}. thanks in advance, George
from what I know there's no such thing and you'd have to iterate thru and pick the unique ones manually. it's not that difficult you know ;)
-
Hello everyone, I have a string array, but may have duplicate strings. Any built-in or smart way to remove the duplicate ones and generate a string array contains only unique ones? For example, the input array is {"abc", "bcd", "abc"}, the unique output array is {"abc", "bcd"}. thanks in advance, George
May be
Array.FindAll
can help you. Check out MSDN for it. Another way could be: 1. Create a list/hashtable. Then loop through the array. 2. Check if the element exists in the list/hashtable. 3. If it does its a duplicate value. Remove it from array. 4. If it doesnt add it to the list/hashtable.C isn't that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions that return void "Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live." - Martin Golding
-
Hello everyone, I have a string array, but may have duplicate strings. Any built-in or smart way to remove the duplicate ones and generate a string array contains only unique ones? For example, the input array is {"abc", "bcd", "abc"}, the unique output array is {"abc", "bcd"}. thanks in advance, George
I dunno why a set class is not included, but it's easy to write one. Just have a list inside, and check if an entry exists before adding it.
Christian Graus Driven to the arms of OSX by Vista.
-
Hello everyone, I have a string array, but may have duplicate strings. Any built-in or smart way to remove the duplicate ones and generate a string array contains only unique ones? For example, the input array is {"abc", "bcd", "abc"}, the unique output array is {"abc", "bcd"}. thanks in advance, George
-
Hi Brij, "some generic for that" -- do you have some more words on this? Or some pseudo code? regards, George
-
Hello everyone, I have a string array, but may have duplicate strings. Any built-in or smart way to remove the duplicate ones and generate a string array contains only unique ones? For example, the input array is {"abc", "bcd", "abc"}, the unique output array is {"abc", "bcd"}. thanks in advance, George
If I'm not mistaken you can use LINQ to select unique values.
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
-
from what I know there's no such thing and you'd have to iterate thru and pick the unique ones manually. it's not that difficult you know ;)
Thanks Igor, Good to know .Net does not provide such a class. :-) regards, George
-
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);
}
} -
If I'm not mistaken you can use LINQ to select unique values.
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
...yes, assuming he's using .net 3+
-
I dunno why a set class is not included, but it's easy to write one. Just have a list inside, and check if an entry exists before adding it.
Christian Graus Driven to the arms of OSX by Vista.
Thanks Christian, What do you mean "have a list inside"? I am talking about string array, I am not sure where is the list you are talking about. Show some pseudo code? regards, George
-
If I'm not mistaken you can use LINQ to select unique values.
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion
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
-
Hello everyone, I have a string array, but may have duplicate strings. Any built-in or smart way to remove the duplicate ones and generate a string array contains only unique ones? For example, the input array is {"abc", "bcd", "abc"}, the unique output array is {"abc", "bcd"}. thanks in advance, 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 newStrs = strs.Distinct(); Hope this will help. LuckyBoy
-
Hello everyone, I have a string array, but may have duplicate strings. Any built-in or smart way to remove the duplicate ones and generate a string array contains only unique ones? For example, the input array is {"abc", "bcd", "abc"}, the unique output array is {"abc", "bcd"}. thanks in advance, 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 :D
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games. -
...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
-
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 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
-
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. :-D
-
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
-
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 :D
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games.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
-
George_George wrote:
I have to use .Net 3.0
Then I can't suggest HashSet. :( But I can suggest my Set class. :-D
What are the advantages of your Set class over .Net Set class? regards, George