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

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 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.
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 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"}
reflection on the process of working in a group. 1.        What was the situation eg: how...
reflection on the process of working in a group. 1.        What was the situation eg: how did you organize your group to make and/or process decisions? 2.        What was your task in the group i.e. what role(s) did you play in the group, and why? 3.        What actions did you take in the group? What went well? What could you have done differently, and why? 4.        What were some of the results of these actions? What further reflections can you...
1. How could you remove the placemat without removing the plate? 2. Suppose you jumped onto...
1. How could you remove the placemat without removing the plate? 2. Suppose you jumped onto a bathroom scale. The scale initially indicates a high weight and then settles down to your actual weight. Is this statement true or false? Could you explain why it is true or why it is false? 3. What is the weight of a person in free fall? Could you please show your work?
• 2. Get all the information from the user using methods Java • B. if the...
• 2. Get all the information from the user using methods Java • B. if the inputs are not given in the proper format the program should prompt user to give the proper input (eg. Name cannot be numbers, age cannot be String)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT