Given a very long list of URLs, find out last unique URL. Only one traversal of all URLs is allowed.
Input: https://www.geeksforgeeks.org http://quiz.geeksforgeeks.org http://qa.geeksforgeeks.org https://practice.geeksforgeeks.org https://ide.geeksforgeeks.org http://www.contribute.geeksforgeeks.org http://quiz.geeksforgeeks.org https://practice.geeksforgeeks.org https://ide.geeksforgeeks.org http://quiz.geeksforgeeks.org http://qa.geeksforgeeks.org https://practice.geeksforgeeks.org Output: http://www.contribute.geeksforgeeks.org
We can solve this problem in one traversal by using Trie with a Doubly Linked List (We can insert and delete in O(1) time). The idea is to insert all URLs into the Trie one by one and check if it is duplicate or not. To know if we have previously encountered the URL, we need to mark the last node of every URL as leaf node. If we encounter a URL for the first time, we insert it into the doubly Linked list and maintain a pointer to that node in linked list in leaf node of the trie. If we encounter a URL that is already in the trie and has pointer to the url in the linked list, we delete the node from the linked list and set its pointer in the trie to null. After all URLs are processed, linked list will only contain the URLs that are distinct and the node at the beginning of the linked list will be last unique URL.
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Find shortest unique prefix for every word in a given list | Set 1 (Using Trie)
- Construct a unique matrix n x n for an input n
- Print unique rows in a given boolean matrix
- Bottom-up traversal of a Trie
- Iterative Preorder Traversal of an N-ary Tree
- Print the DFS traversal step-wise (Backtracking also)
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Union-Find Algorithm | (Union By Rank and Find by Optimized Path Compression)
- Skip List | Set 2 (Insertion)
- Skip List | Set 1 (Introduction)
- Generic Linked List in C
- Self Organizing List | Set 1 (Introduction)
- Self Organizing List : Count Method
- Insertion in Unrolled Linked List