Question

In: Computer Science

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]

Solutions

Expert Solution

Solution Description:

Add elements to input_Array list and then call a function which accepts the array list and returns the array list.

Inside the function declare a temp array list and add the first element of input.

after that run the loop from 0 to size of input_list and check if the current and previous elements are matched or not.if not then add it to temp list.

At last returns the temp Array List .

Code:

import java.util.*;
public class RemoveConsecutiveDuplicates   
{
public static ArrayList<Integer> removeConsecutiveDups(ArrayList<Integer> input_list) //function removeConsecutiveDups() which accepts ArrayList and retunrs it
{
ArrayList<Integer> temp = new ArrayList<Integer>(); //initalize new temp ArrayList
temp.add(input_list.get(0)); //add the starting value of input_list to temp

for(int i = 1; i < input_list.size(); i++) //iterate the loop from 0 to size of ArrayList
{
if(input_list.get(i-1) != input_list.get(i)) //check if the present and previous elements are same if not
{
temp.add(input_list.get(i)); //add to temp list
}
}
return temp; //finally returns the ArrayList
}
public static void main(String args[])
{
ArrayList<Integer> input_list = new ArrayList<Integer>(); //Array List Declaration
input_list.add(1);
input_list.add(2);
input_list.add(3); //add input numbers to input_list
input_list.add(2);
input_list.add(2);
input_list.add(1);
ArrayList<Integer> newList = removeConsecutiveDups(input_list); //call removeConsecutiveDups() functon with passing input_list and store the result
System.out.println("Final List after removing all consecutive duplicates: " + newList); //finally print the result
}
}

Code and Output Screenshots:

Note : if you have any queries please post a comment thanks a lot...always available to help you...


Related Solutions

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
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!
JAVA In this PoD you will use an ArrayList to store different pet names (there are...
JAVA In this PoD you will use an ArrayList to store different pet names (there are no repeats in this list). This PoD can be done in your demo program (where your main method is) – you don’t have to create a separate class for today. Details Create an arraylist of Strings, then using a Scanner object you will first read in a number that will tell you how many pet names (one word) you will add to the arraylist....
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and a method to get all the values of the queue. (This is not writing a file implementing the java class PriorityQueue, but rather you are writing a program that is a priority queue).
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...
JAVA PROGRAMMING ASSIGNMENT - MUST USE ARRAYLIST TO SOLVE. OUTPUT MUST BE EXACT SAME FORMAT AS...
JAVA PROGRAMMING ASSIGNMENT - MUST USE ARRAYLIST TO SOLVE. OUTPUT MUST BE EXACT SAME FORMAT AS SAMPLE OUTPUT. In this assignment, you will create a program implementing the functionalities of a standard queue in a class called Queue3503. You will test the functionalities of the Queue3503 class from the main() method of the Main class. In a queue, first inserted items are removed first and the last items are removed at the end (imagine a line to buy tickets at...
Write a Java program for slidsender and slidreiver 1. Start the program 2. Get the frame...
Write a Java program for slidsender and slidreiver 1. Start the program 2. Get the frame size from the user 3. To create the frame based on the user 4. To send frames to server from the client 5. If your frames reach the server it will send ACK signal to client otherwise it will send NACK signal to 6. Stop the program
show the steps how to get the result of this integral with infinite and -infinite as...
show the steps how to get the result of this integral with infinite and -infinite as boundaries ((x^2*(e^x)/((e^x+1)^2) = pi^2 / 3
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 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