tags:

views:

64

answers:

1

I have an arraylist that contains urls in the form similar to :

stackoverflow.com/questions/ask/hello
stackoverflow.com/questions/ask
stackoverflow.com/questions
stackoverflow.com/

If I only wanted to keep the first one and remove the rest, how would I go about doing that since technically they are not duplicates. I thought of using substring manipulation but not to sure how to implement that.any ideas appreciated.

+1  A: 

Assuming I understand the question correctly, you can accomplish this by looping through your ArrayList, building a list of found domains, and simultaneously outputting a new list only when the found domain is not already a member of that first list.

Or, you could build a dictionary of domain to url by iterating through the ArrayList in reverse. Since a dictionary can only have one value per key, the URLs will overwrite themselves in the dictionary and you will only have one URL per domain. Since you iterated in reverse, you will be left with a dictionary containing the first match in the ArrayList. You could then use LINQ to grab just the values (e.g. MyDictionary.Select(elem => elem.Value)).

An example implementation of the second way I mentioned (in C#, you can convert it) is:

Dictionary<string, string> MyDictionary = new Dictionary<string, string>();
foreach(string Url in MyArrayList.Reverse())
    MyDictionary[Url.Split("/")[0]] = Url;

There are dozens of ways you could accomplish this task. These are just two examples.

David Pfeffer
Thank you sir, I will give that a try.
vbNewbie
Please mark the answer as accepted if it is what you need, otherwise please clarify your question better.
David Pfeffer
I dont wish to be ridiculed again by other uses but bytenik; I gave it a try and please could you look at my code, I am now brain fogged and cannot figure this out For Each link As String In urls 'post.Reverse() For Each part In post If Not dictionary.ContainsKey(link) Then dictionary.Add(link, part) End If Next NextI tried the reverse as well and I am not getting one unique post matching the appropriate url (domain).
vbNewbie
never mind; I am going to take a look at my parsing code again and see if I can clear this before adding to the arraylists. thanks
vbNewbie