💼 Day 54 of #100DaysOfCode Challenge 🚀 📘 Problem: LeetCode #217 – Contains Duplicate 🧠 Topic: HashSet | Array | Data Structures 🔍 Problem Statement: Given an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct. ⚙️ Approach: Used a HashSet to check for duplicates efficiently. Traverse through each number in the array. If the number already exists in the set → return true. Otherwise, add it to the set and continue. If loop ends with no duplicates → return false. 💻 Java Solution: import java.util.HashSet; public class Solution { public boolean containsDuplicate(int[] nums) { HashSet<Integer> set = new HashSet<>(); for (int num : nums) { if (set.contains(num)) { return true; } set.add(num); } return false; } } ⏱ Complexity Analysis: Time: O(n) Space: O(n) 💬 Takeaway: Simple but powerful — using the right data structure like HashSet can make problems easy and efficient to solve. ⚡ #100DaysOfCode #Day54 #LeetCode #Java #CodingChallenge #DSA #HashSet #ProblemSolving #Programmer #TechJourney
Solved LeetCode #217 with HashSet in Java
More Relevant Posts
-
💼 Day 54 of #100DaysOfCode Challenge 🚀 📘 Problem: LeetCode #217 – Contains Duplicate 🧠 Topic: HashSet | Array | Data Structures 🔍 Problem Statement: Given an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct. ⚙ Approach: Used a HashSet to check for duplicates efficiently. Traverse through each number in the array. If the number already exists in the set → return true. Otherwise, add it to the set and continue. If loop ends with no duplicates → return false. 💻 Java Solution: import java.util.HashSet; public class Solution { public boolean containsDuplicate(int[] nums) { HashSet<Integer> set = new HashSet<>(); for (int num : nums) { if (set.contains(num)) { return true; } set.add(num); } return false; } } ⏱ Complexity Analysis: Time: O(n) Space: O(n) 💬 Takeaway: Simple but powerful — using the right data structure like HashSet can make problems easy and efficient to solve. ⚡ #100DaysOfCode #Day54 #LeetCode #Java #CodingChallenge #DSA #HashSet #ProblemSolving #Programmer #TechJourney
To view or add a comment, sign in
-
-
💼 Day 54 of #100DaysOfCode Challenge 🚀 📘 Problem: LeetCode #217 – Contains Duplicate 🧠 Topic: HashSet | Array | Data Structures 🔍 Problem Statement: Given an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct. ⚙ Approach: Used a HashSet to check for duplicates efficiently. Traverse through each number in the array. If the number already exists in the set → return true. Otherwise, add it to the set and continue. If loop ends with no duplicates → return false. 💻 Java Solution: import java.util.HashSet; public class Solution { public boolean containsDuplicate(int[] nums) { HashSet<Integer> set = new HashSet<>(); for (int num : nums) { if (set.contains(num)) { return true; } set.add(num); } return false; } } ⏱ Complexity Analysis: Time: O(n) Space: O(n) 💬 Takeaway: Simple but powerful — using the right data structure like HashSet can make problems easy and efficient to solve. ⚡ #100DaysOfCode #Day54 #LeetCode #Java #CodingChallenge #DSA #HashSet #ProblemSolving #Programmer #TechJourney
To view or add a comment, sign in
-
-
💻 Day 89 of #100DaysOfCoding Challenge 📘 Problem: LeetCode 219 — Contains Duplicate II 🎯 Level: Easy 🧩 Problem Statement: Given an integer array nums and an integer k, return true if there are two distinct indices i and j such that nums[i] == nums[j] and abs(i - j) <= k. 🧠 Approach: Initially, I solved this problem using nested loops, but it resulted in a Time Limit Exceeded (TLE) due to O(n²) complexity. To optimize it, I used a HashMap to store each element and its latest index. This allows checking duplicates within distance k in O(1) time for each iteration — giving an overall O(n) solution. 🕒 Time Complexity: O(n) 💾 Space Complexity: O(n) ✅ Key Takeaway: Sometimes, a brute-force solution may pass small test cases but fail large ones due to inefficiency. Optimizing with appropriate data structures like HashMap or HashSet can significantly improve performance. #LeetCode #Java #HashMap #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
💻 Day 65 of #LeetCode100DaysChallenge Solved LeetCode 219: Contains Duplicate II — a smart problem involving hashing and sliding window logic. 🧩 Problem: Given an integer array nums and an integer k, return true if there exist two distinct indices i and j such that: nums[i] == nums[j], and |i - j| <= k. 💡 Approach — HashMap (Index Tracking): 1️⃣ Use a HashMap to store each number and its most recent index. 2️⃣ For every element nums[i]: If it’s already in the map and i - map.get(nums[i]) <= k, return true. Otherwise, update the index in the map. 3️⃣ If no pair found, return false. ⚙️ Complexity: Time: O(N) — single pass through the array. Space: O(N) — for storing indices in the map. ✨ Key Takeaways: ✅ Strengthened understanding of index-based distance checking. ✅ Efficiently applied hash maps for tracking element positions. ✅ Learned how to implement O(N) duplicate checks with window constraints. #LeetCode #100DaysOfCode #Java #HashMap #SlidingWindow #DSA #ProblemSolving #CodingChallenge #WomenInTech
To view or add a comment, sign in
-
-
𝗕𝗲𝗵𝗶𝗻𝗱 𝘁𝗵𝗲 𝗦𝗰𝗲𝗻𝗲𝘀 𝗼𝗳 𝗝𝗮𝘃𝗮 𝗔𝗿𝗿𝗮𝘆𝗟𝗶𝘀𝘁! Today, I implemented a 𝗗𝘆𝗻𝗮𝗺𝗶𝗰 𝗔𝗿𝗿𝗮𝘆 in Java — building the logic from scratch to understand how ArrayList actually works under the hood. ⚙️ This exercise gave me hands-on exposure to how resizing, shifting, and accessing elements are efficiently handled in dynamic data structures. 𝗛𝗲𝗿𝗲’𝘀 𝘄𝗵𝗮𝘁 𝗜 𝗶𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗲𝗱 👇 🔹 add(element) — to append new items dynamically 🔹 add(index, element) — to insert at a specific position 🔹 get(index) — to fetch values efficiently 🔹 set(index, element) — to replace existing elements 🔹 remove(index/element) — to delete items and shift elements 🔹 contains(element) — to check for existence 🔹 isEmpty() & clear() — to manage and reset the structure 𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻 𝗖𝗼𝗱𝗲 : https://lnkd.in/gXEfwXvf 📘 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆: Recreating ArrayList from scratch deepened my understanding of how memory reallocation, indexing, and dynamic resizing make it one of the most powerful data structures in Java’s Collection Framework. Learning the “how” behind the “what” truly builds stronger foundations. 💪 #Java #DataStructures #ArrayList #CodingJourney #LearningByDoing #SoftwareEngineering #DSA #100DaysOfCode
To view or add a comment, sign in
-
-
📌 Day 27/100 - Generate Parentheses (LeetCode 22) 🔹 Problem: Given n pairs of parentheses, generate all combinations of well-formed (balanced) parentheses. 🔹 Approach: Use backtracking to build strings character-by-character. Keep two counters: open = number of '(' used, close = number of ')' used. You may add '(' while open < n. You may add ')' while close < open (to maintain balance). When the current string length reaches 2*n, add it to the result list. 🔹 Complexity: Time: proportional to the number of valid combinations (Catalan number) — roughly O(4ⁿ / n^(3/2)). Space: output-sensitive — O(n * Cn) for storing results, and O(n) extra for recursion depth. 🔹 Key Learning: Backtracking is ideal when you need to enumerate valid combinations subject to constraints. Always enforce constraints early (here: close < open) to prune invalid branches. Think in terms of state (open/close counts) rather than raw string manipulation. #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA
To view or add a comment, sign in
-
-
💻 Day 53 of #100DaysOfCode – LeetCode #206: Reverse Linked List Today’s challenge was one of the most classic problems in data structures — reversing a linked list. This problem is a great way to strengthen your understanding of pointers and linked list manipulation. 🧩 Problem: Given the head of a singly linked list, reverse it and return the new head. ⚙️ Approach: I used an iterative approach with three pointers: prev, curr, and next. Store the next node Reverse the current node’s pointer Move forward until the list is reversed 🧠 Key Learning: This problem really helped me visualize how pointers work in linked structures — a great exercise for understanding memory links and iterative logic. ✅ Time Complexity: O(n) ✅ Space Complexity: O(1) Here’s my simple Java solution: class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null, curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; } } 🔥 Every day, one step closer to mastering data structures! #Day53 #LeetCode #Java #100DaysOfCode #CodingJourney #LinkedList #DSA #Programming #ReverseLinkedList
To view or add a comment, sign in
-
-
💻 Day 52 of #100DaysOfCodeChallenge Today’s problem: LeetCode 203 – Remove Linked List Elements 🧩 I worked on a Linked List manipulation problem where the goal is to remove all nodes that match a given value. This problem helped strengthen my understanding of pointer handling and dummy node usage in linked lists. 🧠 What I Learned: Handling edge cases where the head itself might need to be removed. Using a dummy node before the head to simplify pointer operations. How current and next pointers work together to modify a linked list safely. 🧩 Java Solution: class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode current = dummy; while (current.next != null) { if (current.next.val == val) current.next = current.next.next; else current = current.next; } return dummy.next; } } ⏱️ Complexity: Time: O(n) Space: O(1) Every day, I’m getting more confident with data structures and linked list traversal logic. On to the next challenge 🚀 #Day52 #LeetCode #Java #100DaysOfCode #CodingChallenge #LinkedList #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Just Solved LeetCode #1367 — Linked List in Binary Tree 📘 Problem: Given the head of a linked list and the root of a binary tree, determine whether all the elements of the linked list appear as a downward path in the binary tree. Downward means starting from any node and moving only to child nodes. Example: Input → head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] Output → true Explanation → Nodes in blue form a subpath in the binary tree. 🧠 My Approach: I used recursion to check whether the linked list sequence can be found starting from any node in the binary tree. 1️⃣ For each node in the tree, check if the list starting from `head` matches the downward path from that node. 2️⃣ If not, recursively check the left and right subtrees. 3️⃣ Used a helper function `checkPath()` to verify if a valid path continues as the recursion goes deeper. 💡 What I Learned: ✅ How to combine linked list and binary tree traversal logic ✅ How recursion can efficiently check multiple starting points ✅ Improved my understanding of tree path traversal and backtracking logic #LeetCode #Java #DSA #BinaryTree #LinkedList #CodingUpdate #LearningByDoing
To view or add a comment, sign in
-
-
✅ Day 76 of LeetCode Medium/Hard Edition Today’s challenge was “Linked List in Binary Tree” — an elegant recursion and tree-traversal problem that blends linked list navigation with depth-first search on a binary tree 🌳🔗⚡💡 📦 Problem: You're given the head of a linked list and the root of a binary tree. Your task is to check whether the entire linked list appears as a downward path in the binary tree. A downward path means you may start at any node, but can only move to left or right children in each step. 🔗 Problem Link: https://lnkd.in/gwhvXVK4 ✅ My Submission: https://lnkd.in/gjqiBfJG 💡 Thought Process: A naive approach would try to follow the linked list from every tree node using repeated traversal — but without careful handling, this leads to redundant work. The optimized idea is to use DFS with two layers: 1️⃣ Search layer → Try starting the match from every node in the tree 2️⃣ Match layer → Check if the linked list matches a downward path from that node We define two functions: helper() → explores the tree to find a potential starting point match() → attempts to follow the linked list downward If at any node the values match and the entire list gets consumed, we return true. This structure avoids unnecessary recomputation and ensures clean, efficient traversal. ⚙️ Complexity: ⏱ Time: O(N × M) in the worst case (N = tree nodes, M = list length) 💾 Space: O(H) due to recursion stack (H = tree height) #LeetCodeMediumHardEdition #100DaysChallenge #DFS #BinaryTree #LinkedList #Java #DSA #ProblemSolving #LearnEveryday
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development