Wednesday 8 January 2014

Java: Merge two unsorted arrays and remove duplicates

Merge two unsorted array and remove the duplicate from the resultant array. example
Array1 = {"are","you","there"}
Array2={"how","are","you"}
output={"how","are","you","there"}


public class Ques2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
          String[] Array1 = {"how","are","you"};
    String[] Array2 = {"are","you","there","now"};
    
    HashMap ht=new HashMap();
    for(int i=0;i<Array1.length;i++)
    {
        if(!ht.containsValue(Array1[i])) {
           ht.put(Array1[i], Array1[i]);
        }
    }
     for(int i=0;i<Array2.length;i++)
    {
        if(!ht.containsValue(Array2[i])) {
           ht.put(Array2[i], Array2[i]);
        }
    }
   //  for(int i=0;i<ht.size();i++){
         System.out.println(ht.values());
   //  }
    }
}

No comments:

Post a Comment