WAP To Remove Consecutive Duplicates from Array

Remove Consecutive Duplicates from Array: This Java program aims to remove consecutive duplicates from an input array of integers and print the modified array. It demonstrates an algorithmic approach to achieve this task using two ArrayLists.

Remove Consecutive Duplicates from the Array

package com.softwaretestingo.interviewprograms;
import java.util.ArrayList;
import java.util.List;
public class InterviewPrograms13 
{
	/*
	 * Input = [1,2,2,3,4,5,5,3] 
	 * Output = [1,3,4,3]
	 */
	public static void main(String[] args) 
	{
		int Input[] = {1,2,2,3,4,5,5,3};
		List<Integer> list= new ArrayList<>();
		List<Integer> list1= new ArrayList<>();
		for (int i = 0; i <= Input.length-1; i++) 
		{
			list.add(Input[i]);
		}
		for (int j = 0; j <= list.size()-1; j++)
		{
			if(j==list.size()-1)
			{
				if(list.get(j).equals(list.get(j-1)))
				{
				}
				else 
				{
					list1.add(list.get(j));
				}
				break;
			}
			if(list.get(j).equals(list.get(j+1)))
			{
				j=j+1;
			}
			else if ( j!=0 && list.get(j).equals(list.get(j-1)) ) 
			{
				//j=j+1;
			}
			else 
			{
				list1.add(list.get(j));
			}
		}
		for(Integer d:list1)
		{
			System.out.print(d+",");
		}
	}
}

Output:

1,3,4,3,

Here’s a step-by-step explanation of how the program works:

  • The program defines a class named InterviewPrograms13.
  • Inside the class, the main method starts the execution of the program.
  • An input array Input containing integers is initialized.
  • Two ArrayLists, list and list1, are created to store the elements of the input array and the result after removing consecutive duplicates, respectively.
  • The program uses a for loop to iterate through each element in the Input array and adds them to the list.
  • Another for loop iterates through each element in the list.
  • If the current element is the last element in the list, and it’s not equal to the previous element, it adds it to the list1 (to handle the last element).
  • If the current element is equal to the next element, it increments the loop index j to skip the next element and avoid adding duplicates to list1.
  • If the current element is not equal to the previous element and not equal to the next element, it adds it to list1.
  • Finally, the program prints the modified list1, which contains the elements after removing consecutive duplicates.

In summary, this program demonstrates how to remove consecutive duplicate elements from an input array of integers using two ArrayLists and appropriate loop conditions. The final output is the modified array, which contains elements [1, 3, 4, 3] when the input is [1, 2, 2, 3, 4, 5, 5, 3].

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment