From fd3386a0dbfb8c70d5226909cf462d515f3d96b7 Mon Sep 17 00:00:00 2001 From: Taranjeet Singh Kalsi Date: Tue, 25 Oct 2022 17:49:03 +0530 Subject: [PATCH] Removed extra loop in Anagram.java (#3654) --- src/main/java/com/thealgorithms/strings/Anagrams.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/Anagrams.java b/src/main/java/com/thealgorithms/strings/Anagrams.java index 6ac10442..1d61e42e 100644 --- a/src/main/java/com/thealgorithms/strings/Anagrams.java +++ b/src/main/java/com/thealgorithms/strings/Anagrams.java @@ -121,13 +121,8 @@ public class Anagrams { for (char c : t.toCharArray()) { kk.put(c, kk.getOrDefault(c, 0) + 1); } - // It checks for equal frequencies - for (char c : nm.keySet()) { - if (!nm.get(c).equals(kk.get(c))) { - return false; - } - } - return true; + // It checks for equal frequencies by comparing key-value pairs of two hashmaps + return nm.equals(kk); } } }