In: Computer Science
Question about Java ArrayList:
how to delet congestive number:
eg : [1 2 2 3 2 2 1] -----> [1 2 3 2 1]
how to get the number that only in one list:
eg:
[2 2 1], [ 4 4 2 2], [8 8 4 4 2] -------->[1,8]
[8 8 4 4 2], [4 4 2 2], [2 2 1] --------->[1,8]
Thanks in advance
First Program
//importing the required packages
import java.util.*;
// main class is named Distinct
public class Distinct {
   public static void main(String args[])
   {
       //Creating an ArrayList
object
       List<Integer> array = new
ArrayList<Integer>();
       //Adding the elements in to the
list.
       array.add(1);//adding 1 to the
list
       array.add(2);//adding 2 to the
list
       array.add(2);//adding 2 to the
list
       array.add(3);//adding 3 to the
list
       array.add(2);//adding 2 to the
list
       array.add(2);//adding 2 to the
list
       array.add(1);//adding 1 to the
list
       //printing tthe array content
       System.out.println("List of the
numbers with repetitions: "+array);
       //Creating an Set Object
newArray
       //Sets do not contain duplicate
elements.
       //We are passing our array object
as argument.
       Set<Integer> newArray = new
LinkedHashSet<Integer>(array);
      
       //Removing the all the elements
added to the array object.
       array.clear();
       //Now we are adding all the
elements in the set object newArray to the array object
       array.addAll(newArray);
       //printing all the elements in the
array.
       System.out.println("List with
repitions deleted "+array);
   }
}
OUTPUT: -

Second Program
//importing all the package required
import java.util.*;
//main class is named OnlyOne
public class OnlyOne {
  
   public static void main(String[] args) {
   //Creating the ArrayList objects for the three lists
with names array1, array2, array3  
   List<Integer> array1 = new   
ArrayList<Integer>();
   List<Integer> array2 = new
ArrayList<Integer>();
   List<Integer> array3 = new
ArrayList<Integer>();
   //Adding elements to the arrays
   array1.add(2); // adding 2 to array1
   array1.add(2);// adding 2 to array1
   array1.add(1);// adding1 to array1
   //Removing duplicate items in the array1 using set
object.
   Set<Integer> newArray1 = new
LinkedHashSet<Integer>(array1);
   //removing all the elements in array1
   array1.clear();
   //adding all the set object elements to the array1
object
   array1.addAll(newArray1);
  
   //we do same process for object array2 and
array3.
  
   array2.add(4);
   array2.add(4);
   array2.add(2);
   array2.add(2);
  
   Set<Integer> newArray2 = new
LinkedHashSet<Integer>(array2);
   array2.clear();
   array2.addAll(newArray2);
  
   array3.add(8);
   array3.add(8);
   array3.add(4);
   array3.add(4);
   array3.add(2);
  
   Set<Integer> newArray3 = new
LinkedHashSet<Integer>(array3);
   array3.clear();
   array3.addAll(newArray3);
  
   //printing the contents of all the array
objects.
   System.out.println(array1+" "+array2+"
"+array3);
   //create a new ArrayList object to store elements that
exist in only one list.
   List<Integer> uncommon = new
ArrayList<Integer>();
  
   //check if the element in array1 exists in array2 or
not.
   //If it's not there then add the element to
uncommon
    for(int i : array1) {
       if(!array2.contains(i))
          
uncommon.add(i);
   }
    //check if the element in array3 exists in
array2 or not.
    //If it's not there then add the element to
uncommon
   for( int i : array3 ) {
       if(!array2.contains(i))
          
uncommon.add(i);
   }
  
   //print the elements in the uncommon list
   System.out.println( "Elements that are only in one
list. "+ uncommon);
   }
}
OUTPUT:-
