Solved LeetCode 1207 with HashMap and HashSet in Java

🚀 Day 28 of #100DaysOfCode Today’s challenge was all about understanding frequency patterns in arrays — a simple yet powerful concept in hashing 🔍 LeetCode 1207 – Unique Number of Occurrences 🔢 📌 Problem Summary: Given an integer array, determine whether each value appears a unique number of times. In other words, 👉 No two numbers should share the same frequency. 💡 Example: Input: [1,2,2,1,1,3] Output: true Because frequencies are: 1 → 3 times 2 → 2 times 3 → 1 time All unique ✔️ 🧠 My Approach: I solved it using a combination of HashMap + HashSet: 1️⃣ Count the occurrences of each number using a HashMap. 2️⃣ Insert all frequency values into a HashSet. 3️⃣ If the size of the HashSet equals the size of the map → all frequencies are unique ✔️ 4️⃣ Otherwise → duplicates exist ❌ A clean and efficient hashing technique. ⚙️ Complexity: Time: O(n) ⏱️ Space: O(n) 💾 (storing frequencies) 💡 Key Learning: HashMaps aren’t just for storing counts—they can reveal patterns, validations, and uniqueness constraints when combined with sets. This problem reinforced how frequency-based logic solves many real-world scenarios like: ✔ Detecting duplicates ✔ Comparing patterns ✔ Character/string analysis ✔ Data validation 🔥 Result: Code ran successfully with 0 ms Runtime, accepted on the first attempt! Small challenge, clean logic, satisfying finish 💪 On to the next one! 🚀 #100DaysOfCode #LeetCode #HashTable #Java #ProblemSolving #DSA #CodingJourney

  • text

To view or add a comment, sign in

Explore content categories