• 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 Duplicate Characters from String

WAP to Remove Consecutive Duplicate Characters from String

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

What We Are Learn On This Post

  • Remove Consecutive Duplicate Characters from String

This Java program aims to remove consecutive duplicate characters from a given string. It utilizes a stack data structure to achieve this.

Remove Consecutive Duplicate Characters from String

package com.softwaretestingo.interviewprograms;
import java.util.Stack;
public class InterviewPrograms39 
{
	/*
	 * Input string : weelccoommee hhoommeee 
	 * Output string : welcome home
	 */
	public static void main(String[] args) 
	{
		String s = "weelccoommee hhoommeee";
		Stack<Character> st = new Stack<>();
		st.push(s.charAt(0));
		for (int i = 1; i<s.length(); i++ ) 
		{
			if (s.charAt(i-1) != s.charAt(i))
				st.push(s.charAt(i));
		}
		for ( Character c : st )
			System.out.print(c) ;
	}
}

Output

welcome home

Here’s a breakdown of the program:

  1. Define the main method:
    • The entry point of the program is the main method.
  2. Initialize the input string and create a stack:
    • The string s is initialized with the value “weelccoommee hhoommeee”.
    • A stack of characters named st is created.
  3. Process the string and remove consecutive duplicates:
    • The program iterates through each character of the string using a for loop, starting from the second character.
    • It compares the current character with the previous character using the condition s.charAt(i-1) != s.charAt(i).
    • If the current character is different from the previous character, it is pushed onto the stack using st.push(s.charAt(i)).
  4. Print the non-duplicate characters:
    • The program then uses an enhanced for loop to iterate over the characters in the stack.
    • Each character is printed using System.out.print(c).

The program outputs the non-duplicate characters from the original string. In this case, the output will be “welcome home”, where consecutive duplicate characters have been removed.

Alternative 1:

This Java program aims to remove consecutive duplicate characters from a given input string using regular expressions.

package com.softwaretestingo.interviewprograms;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class InterviewPrograms39_1 
{
	/*
	 * Input string : weelccoommee hhoommeee 
	 * Output string : welcome home
	 */
	public static void main(String[] args) 
	{
		String s = " weelccoomme hhommeee " ;
		Pattern pattern = Pattern.compile("(.)\\1*");
		Matcher m= pattern.matcher ( s ) ;
		String output = "";
		while ( m.find())
		{
			output= output + m.group().charAt(0);
		}
		System.out.println ( output ) ;
	}
}

Output

welcome home

Here’s a breakdown of the program:

  1. Define the main method:
    • The entry point of the program is the main method.
  2. Initialize the input string and compile the regex pattern:
    • The string s is initialized with the value ” weelccoomme hhommeee “.
    • A regex pattern is created using Pattern.compile(“(.)\\1*”). This pattern looks for consecutive repeated characters.
  3. Create a Matcher object and find matches:
    • A Matcher object named m is created by applying the pattern on the input string s.
    • The m.find() method is used to find occurrences of the pattern in the input string.
  4. Build the output string:
    • While the m.find() method continues to find matches, the program extracts the matched group using m.group().
    • The first character of each matched group is extracted using charAt(0) and added to the output string.
  5. Print the output:
    • The program prints the final output string, which contains the non-duplicate characters from the original string.

The program outputs “welcome home”, where consecutive duplicate characters have been removed using regular expressions.

    Decimal to Binary How To Conversion in Java
    Convert Decimal to Binary
    WAP To Reverse String Without Using Inbuilt Functions
    WAP To Reverse String Without Using Inbuilt Functions
    WAP to Remove Characters That Appear More Than K Times
    WAP to Find Kth Smallest Element in an Array
    WAP For Find Maximum Possible Combinations Of Triangle
    WAP For Find Maximum Possible Combinations Of Triangle
    Patterns Program In Java
    Patterns Program In Java
    WAP to Reverse Words Without Changing Digits Position
    WAP to Reverse Words Without Changing Order & Digits Position
    WAP to Split An Array Into Two Equal Sum Subarrays
    WAP to Split An Array Into Two Equal Sum Subarrays
    Java Program Tutorial For Practice
    Java Program – Java Programming Examples
    WAP to Remove Characters That Appear More Than K Times
    WAP to Remove Characters That Appear More Than K Times
    Java Program Interview Concatenate Characters From Each String
    WAP to Concatenate Characters From Each 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