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 achieving this task using two Array Lists.

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,

Avatar for Softwaretestingo Editorial Board

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