Question

In: Computer Science

Question about Java ArrayList: how to delet congestive number: eg : [1 2 2 3 2...

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

Solutions

Expert Solution

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:-


Related Solutions

how to use java ArrayList to get the result? removing consecutive duplicates eg [1 2 2...
how to use java ArrayList to get the result? removing consecutive duplicates eg [1 2 2 3 2 2 1] ------->[1 2 3 2 1]
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c]...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c] , [d,e], [f] ] ----> [ [a,d,f], [a,e,f], [b,d,f], [b,e,f], [c,d,f], [c,e,f]] [ [a,b], [a,b,c]] ----->[[a,a],[a,b],[a,c], [b,a],[b,b],[b,c] assuming abc are integer Thanks in advance!
Hello I have a question about Java. example So when you put “sum 1 2 3”...
Hello I have a question about Java. example So when you put “sum 1 2 3” in console, you do not separate those. Need a space between sum 1, 2 in the console. What is Sum? sum 1 2 3 6 (answer) sum 123456 21 sum 100 2000 3000 5100 sum 20 50 40 1 111 public static void main (String[] args) { System.out.Println(“What is Sum?”); String a=“”; a = scnr.nextLine(); String[] b = a.split(“”); if(b[0].equals(“sum”) { } Please do...
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings...
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings representing the names of staff members. The constructor takes an ArrayList of String names as a parameter. In addition to the constructor, you need to implement the following methods The methods 1. public boolean equals(Staff other) - Determines if the other Staff contains all the same elements in the same order as this Staff 2. public boolean sameContents(Staff other) - Determines if the other...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
2. Answer the following question (a) Write a Java program to find the number of integers...
2. Answer the following question (a) Write a Java program to find the number of integers within the range of two specified numbers and that are divisible by another number. For example, x = 5, y=20 and p =3, find the number of integers within the range [x, y] and that are divisible by p. (b) Write explicitly on the following OOP concepts: i. Encapsulation ii. Inheritance iii. Polymorphism (c) Develop a Java application that computes the two roots of...
need to output this in java Enter an integer: 5 1 2 2 2 3 3...
need to output this in java Enter an integer: 5 1 2 2 2 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 3 3 3 3 3 2 2 2 1 (in the shape of a diamond) please help me with the correct code, thanks
JAVA PROGRAMMING 1)BuildLists: Write a program to create an ArrayList<Integer>. Fill it with numbers from 1...
JAVA PROGRAMMING 1)BuildLists: Write a program to create an ArrayList<Integer>. Fill it with numbers from 1 to 1000. Then remove every even number. Then remove every multiple of 3 remaining Then remove every multiple of 5 Then remove every multiple of 7 Then sum the array, and print.
JAVA JAVA JAVA JAVA, How to determine if there is more than one number that appeared...
JAVA JAVA JAVA JAVA, How to determine if there is more than one number that appeared the X amount of times. (For example there are 3 numbers which occured 50 times so I want the output to show which 3 numbers occured 50 times) This is the code import java.util.Random; public class MostLeastOccurring { public static void main(String[] args) { Random random = new Random(); int x = 1000; int[] array = new int[x]; for (int i = 0; i...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT