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());
   //  }
    }
}

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete