From 8f344ca0f71ed6f14ebcf84a7b466df60c72bb88 Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Tue, 1 Aug 2023 15:22:01 +0530 Subject: [PATCH] BAEL-6841 (#14502) Improvement article - "Check if a String Has All Unique Characters in Java" --- .../com/baeldung/uniquecharcheck/UniqueCharChecker.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/uniquecharcheck/UniqueCharChecker.java b/core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/uniquecharcheck/UniqueCharChecker.java index fefacb92d0..877c995df5 100644 --- a/core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/uniquecharcheck/UniqueCharChecker.java +++ b/core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/uniquecharcheck/UniqueCharChecker.java @@ -34,9 +34,11 @@ public class UniqueCharChecker { char[] chars = str.toUpperCase().toCharArray(); Set set = new HashSet <>(); for (char c: chars) { - set.add(c); + if (!set.add(c)) { + return false; + } } - return set.size() == str.length(); + return true; } public static boolean useStreamCheck(String str) {