• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » WAP To Remove Consecutive Duplicates from Array

WAP To Remove Consecutive Duplicates from Array

Last Updated on: July 27, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Remove Consecutive Duplicates from the 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].

    WAP to Remove Digits Only From a String
    WAP to Remove Digits Only From a String
    Print Character And Count Of a Word
    Write a Java Program[ abbcccdeee –>a1b2c3d1e3]
    String Programs in Java For Interview
    String Programs in Java For Interview
    WAP to Replace Last Two Special Character With Dots
    WAP to Replace Last Two Special Character With Dots
    WAP to Reverse String Based on Commas
    WAP to Reverse String Based on Commas
    Substring Matching In Java
    Substring Matching In Java
    WAP For Find Maximum Possible Combinations Of Triangle
    WAP For Find Maximum Possible Combinations Of Triangle
    WAP to Split An Array Into Two Equal Sum Subarrays
    WAP to Split An Array Into Two Equal Sum Subarrays
    Product of Array Except Itself Program
    Product of Array Except Itself
    WAP To Find Out First Repeated Character In A String
    WAP To Find Out First Repeated Character In A String

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers