Crackingcodinginterview (2.1) removes duplicate elements from the shortlist

Source: Internet
Author: User
Why? Importjava. util. Collections list; importjava. util. Iterator; importjava. util. Collections; importjav

2.1 Write code to remove duplicates from an unsorted linked list. follow up How wocould you solve this problem if a temporary buffer is not allowed? Import java. util. Collections list; import java. util. Iterator; import java. util. Collections; import jav

2.1 Write code to remove duplicates from an unsorted linked list.
FOLLOW UP

How wocould you solve this problem if a temporary buffer is not allowed?

import java.util.LinkedList;import java.util.Iterator;import java.util.Collections;import java.util.Hashtable;public class Solution{//brute-force time complexity:O(n^2) space complexity:O(1)public static void removeDuplicate1(LinkedList
 
   list){for(int i=0;i < list.size()-1;i++)for(int j=i+1;j < list.size();)if(list.get(i) == list.get(j))list.remove(j);elsej++;}//could't keep order time complexity:O(nlogn) space complexity:O(1)public static void removeDuplicate2(LinkedList
  
    list){//sortCollections.sort(list);if(list.size() >= 2){for(int i=0;i < list.size()-1;){if(list.get(i) == list.get(i+1))list.remove(i+1);elsei++;}}}//1.keep order 2.time complexity:O(n) 3.space complexity:O(n): (worst case)public static void removeDuplicate3(LinkedList
   
     list){Hashtable
    
      hash = new Hashtable
     
      ();//lookup hashtable to delete repeat elementsfor(int i=0;i < list.size();){if(hash.containsKey(list.get(i)))list.remove(i);else{hash.put(list.get(i), "");i++;}}}private static void printLinkedList(LinkedList
      
        list){Iterator it = list.iterator();while(it.hasNext()){System.out.print((Integer)it.next()+" ");}System.out.println();}public static void main(String[] args){LinkedList
       
         list = new LinkedList
        
         ();list.add(6);list.add(2);list.add(2);list.add(3);list.add(1);list.add(4);list.add(2);list.add(3);list.add(7);list.add(2);list.add(2);list.add(10);Solution.printLinkedList(list);Solution.removeDuplicate3(list);Solution.printLinkedList(list);}}
        
       
      
     
    
   
  
 

1. brute-force time complexity: O (n ^ 2) space complxity: O (1). The output elements are in the original order.

2. sort: time complexity: O (nlogn) space complexity: O (1). The output element is the sorted result.

3. hashtable: time complexity: O (n) space complexity: O (n). The output elements are in the original order.

Similar Problem: http://blog.csdn.net/u011559205/article/details/38125405

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.