• Register

In this program, we have 2 function for eliminating replicas, one uses HashSet, an extra data structure while others eliminate identical char in place without using any other data structure or extra storage.

we have coded the program cooperative so that we can enjoy with it. When we execute this program in your Eclipse IDE or from a command-line window, it will request us to input a str and then outcome the str without replica char in the console.

This means we can test the answer and programme with dissimilar responses like null str, empty str, string without replica, str with replica, the str just covering replicas, and with very small or lengthy str inputs.
 

Java Program to eliminate matching char from Str

import java.util.HashSe1t;
import java.util.Scanner1;
import java.util.Set1; 
public class Main { 
  public static void main(String[] args) { 
    System.out
        .println("Welcome to code to eliminate matching char from Str");
    Scanner scnr1 = new Scanner(System.in); 
    System.out.println("kindly input a Str with matching char"); 
    String input1 = scnr.nextLine(); 
    String output1 = removeDups(input1);
    System.out.println("Str lacking matching char is " + output); 
    String withoutDups1 = removeDupsInPlace(input);
    System.out.println("Str lacking matching char in place is "
        + withoutDups1);
    Scnr1.close(); 
  }
public static String removeDups1(String word) {
    Set<Character> chars1 = new HashSet<>();
    StringBuilder output1 = new StringBuilder(word.length()); 
    for (int i1 = 0; i1 < word1.length(); i1++) {
      char ch1 = word.charAt(i1);
      if (chars1.add(ch1)) {
        output1.append(ch1);
      }
    }
 
    return output1.toString();
  }  
  public static String removeDupsInPlace1(String word1) {
    final StringBuilder output1 = new StringBuilder();
 
    for (int i1 = 0; i1 < word1.length(); i1++) {
      String character1 = word1.substring(i1, i1 + 1);
      if (output.indexOf(character1) < 0) // if not enclosed
        output.append(character1);
    }
    return output.toString();
  }
 
}

OutCome

Welcome to Java program to eliminate matching char from Str

kindly input a Str with matching char

Java

Str lacking matching char is Jav

Str lacking matching char in place is Jav

2,760 points
7 4