From ec3fcc078d9018d7f7ae779366741121679bf95b Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Sat, 6 Jan 2024 03:20:08 +0100 Subject: [PATCH] =?UTF-8?q?[rm-char-in-other-str]=20BAEL-7355=20Remove=20c?= =?UTF-8?q?haracters=20from=20a=20string=20that=E2=80=A6=20(#15531)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [rm-char-in-other-str] BAEL-7355 Remove characters from a string that are present in a second string * [rm-char-in-other-str] shorten the package name --- .../RemoveCharsInAnotherStringUnitTest.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 core-java-modules/core-java-string-algorithms-4/src/test/java/com/baeldung/string/rmcharsinanotherstring/RemoveCharsInAnotherStringUnitTest.java diff --git a/core-java-modules/core-java-string-algorithms-4/src/test/java/com/baeldung/string/rmcharsinanotherstring/RemoveCharsInAnotherStringUnitTest.java b/core-java-modules/core-java-string-algorithms-4/src/test/java/com/baeldung/string/rmcharsinanotherstring/RemoveCharsInAnotherStringUnitTest.java new file mode 100644 index 0000000000..33bd687a4a --- /dev/null +++ b/core-java-modules/core-java-string-algorithms-4/src/test/java/com/baeldung/string/rmcharsinanotherstring/RemoveCharsInAnotherStringUnitTest.java @@ -0,0 +1,75 @@ +package com.baeldung.string.rmcharsinanotherstring; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.jupiter.api.Test; + +public class RemoveCharsInAnotherStringUnitTest { + + private static final String STRING = "a b c d e f g h i j"; + private static final String OTHER = "bdfhj"; + + String nestedLoopApproach(String theString, String other) { + StringBuilder sb = new StringBuilder(); + for (char c : theString.toCharArray()) { + boolean found = false; + for (char o : other.toCharArray()) { + if (c == o) { + found = true; + break; + } + } + if (!found) { + sb.append(c); + } + } + return sb.toString(); + } + + String loopAndIndexOfApproach(String theString, String other) { + StringBuilder sb = new StringBuilder(); + for (char c : theString.toCharArray()) { + if (other.indexOf(c) == -1) { + sb.append(c); + } + } + return sb.toString(); + } + + String hashSetApproach(String theString, String other) { + StringBuilder sb = new StringBuilder(); + Set set = new HashSet<>(other.length()); + for (char c : other.toCharArray()) { + set.add(c); + } + + for (char i : theString.toCharArray()) { + if (!set.contains(i)) { + sb.append(i); + } + } + return sb.toString(); + } + + @Test + void whenUsingTheNestedLoopApproach_thenGetExpectedResult() { + String result = nestedLoopApproach(STRING, OTHER); + assertEquals("a c e g i ", result); + } + + @Test + void whenUsingTheLoopAndIndexOfApproach_thenGetExpectedResult() { + String result = loopAndIndexOfApproach(STRING, OTHER); + assertEquals("a c e g i ", result); + } + + @Test + void whenUsingTheHashSetApproach_thenGetExpectedResult() { + String result = hashSetApproach(STRING, OTHER); + assertEquals("a c e g i ", result); + } + +} \ No newline at end of file