🚀 Day #83/100 – 𝐈𝐧𝐭𝐞𝐫𝐬𝐞𝐜𝐭𝐢𝐨𝐧 𝐨𝐟 𝐓𝐰𝐨 𝐀𝐫𝐫𝐚𝐲𝐬 Today’s problem was Intersection of Two Arrays — a simple and clean problem focused on sets and uniqueness. 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: Using a HashSet makes it easy to handle duplicates and find common elements efficiently. 💡 𝐂𝐨𝐫𝐞 𝐈𝐝𝐞𝐚: Store elements of the first array in a set Traverse the second array and check if elements exist in the set Use another set to store unique intersection results 𝐖𝐡𝐲 𝐢𝐭 𝐰𝐨𝐫𝐤𝐬? Sets automatically handle duplicates and provide O(1) lookup time, making the solution efficient. ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Add all elements of nums1 into a set Traverse nums2: If element exists → add to result set Convert result set to array ⏱️ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: O(n + m) 📦 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: O(n) #Day83 #100DaysOfCode #Java #DSA #LeetCode #HashSet #CodingJourney
Intersection of Two Arrays Solution with HashSet
More Relevant Posts
-
🚀 Day 28/100 – LeetCode Challenge Today’s problem: Intersection of Two Arrays (349) 📌 Problem Summary: Given two arrays, find their intersection such that each element in the result is unique. 💡 My Approach: Used HashSet to store elements of the first array Checked elements of the second array using contains() Stored common elements in another set to avoid duplicates Converted the result set into an array 🧠 What I learned today: Efficient use of HashSet for fast lookup (O(1)) How to handle duplicate elements easily Importance of choosing the right data structure Writing clean and optimized code instead of using nested loops 💻 Code Approach (Java): Used sets to ensure uniqueness and optimize performance 🚀 Consistency matters more than perfection 🔑 #Day28 #100DaysOfCode #LeetCode #DSA #Java #CodingJourney #PlacementPreparation
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 86/100 – 𝐋𝐨𝐧𝐠𝐞𝐬𝐭 𝐂𝐨𝐧𝐬𝐞𝐜𝐮𝐭𝐢𝐯𝐞 𝐒𝐞𝐪𝐮𝐞𝐧𝐜𝐞 Today’s problem was Longest Consecutive Sequence — a great example of optimizing from brute force to an efficient solution using hashing. 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: Sorting would take O(n log n), but we can solve this in O(n) using a HashSet. 💡 𝐂𝐨𝐫𝐞 𝐈𝐝𝐞𝐚: Store all elements in a set for O(1) lookup Only start counting when the current number is the start of a sequence 𝐖𝐡𝐲 𝐢𝐭 𝐰𝐨𝐫𝐤𝐬? This ensures we only count each sequence once, avoiding unnecessary repeated work. ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Add all elements to a HashSet Iterate through the set: If (num - 1) doesn’t exist → start a sequence Keep increasing and count streak Track maximum length ⏱️ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 📦 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) #Day86 #100DaysOfCode #Java #DSA #LeetCode #HashSet #CodingJourney
To view or add a comment, sign in
-
-
Day 31 of #50DaysOfDSA Solved: Merge Two Sorted Lists - LeetCode 21 Approach: Iterative, no dummy node Handle the head first, compare both lists, whichever is smaller becomes the head. Then traverse with a current pointer, always attaching the smaller value and advancing that list. Once one runs out, attach the remainder. Time: O(m + n) | Space: O(1) Most solutions use a dummy node to simplify head selection. This one handles it explicitly, a little more setup, but zero extra node overhead. Sometimes the manual approach teaches you more. #50DaysOfDSA #DSA #LeetCode #LinkedList #Java #CodingChallenge #WomenInTech #SoftwareDevelopment
To view or add a comment, sign in
-
-
🚀 Day 64/100 Today’s problem: Find all strings that are substrings of another word 🧠 What I learned: - How to compare strings using nested loops - Using ".contains()" to check substrings efficiently - Importance of breaking early to optimize performance - Strengthening problem-solving with brute-force approach 💡 Key Insight: Sometimes simple solutions (O(n²)) are enough when constraints are small. No need to overcomplicate! 🔁 Consistency > Perfection #Day64 #DSA #Java #CodingJourney #Consistency #KeepLearning #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 14 of #DSAwithEdSlash Solved: Product of Array Except Self 🔥 Today’s problem was all about optimizing space and avoiding division — a classic twist! 💡 Learned how to efficiently use prefix and suffix products to achieve O(n) time complexity. ✅ Key Takeaways: No division approach 🚫➗ Smart use of left & right product arrays Space optimization techniques 📊 Result: ⚡ Runtime: 2 ms (Beats 94.97%) 💾 Memory: 64.10 MB (Beats 95.04%) Consistency is building confidence day by day 💪 #Day14 #DSA #LeetCode #Java #ProblemSolving #CodingJourney #Consistency #PlacementPrep
To view or add a comment, sign in
-
-
🚀 Day 52 of #100DaysOfLeetCode Solved: Daily Temperatures (Monotonic Stack) Today’s focus was on understanding how to optimize from a brute-force O(n²) approach to an efficient O(n) solution using a stack. Key takeaways: Learned how to identify “next greater element” patterns Understood how monotonic stacks help avoid redundant work Practiced writing clean and optimized code Consistency is starting to pay off. Onto the next one. #DSA #Java #CodingJourney #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
Day 5/30 of #30DaysOfDSA 🔹 Problem: Decode String 💡 Approach: Used a stack-based approach to handle patterns like k[encoded_string], especially nested ones. Stored counts and previous strings, and built the result step-by-step while traversing. ⚡ What I Learned: • Handling nested structures using stacks • Efficient string building • Optimizing parsing logic Complexity: O(n) #DSA #Coding #Java #Consistency
To view or add a comment, sign in
-
-
🚀 Day 48 of My DSA Journey 🧩 Problem: 2799. Count Complete Subarrays in an Array 💡 Approach: First, find total distinct elements in the array using a HashSet. Then check every subarray and track its distinct elements. If it matches the total distinct count → it’s a complete subarray. ⏱ Time Complexity: O(n²) 📌 Key Takeaway: Using sets makes it easy to track distinct elements and compare efficiently. #Day48 #DSA #LeetCode #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 55 of #100DaysOfCode Solved 147. Insertion Sort List on LeetCode 🔗 🧠 Key Insight: We apply the classic Insertion Sort, but on a linked list instead of an array. The challenge is handling pointer manipulation efficiently. ⚙️ Approach: 1️⃣ Create a dummy node to act as the start of the sorted list 2️⃣ Traverse the original list node by node 3️⃣ For each node: Find its correct position in the sorted part Insert it there by updating pointers 🔁 This builds a sorted list incrementally ⏱️ Time Complexity: O(n²) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #LinkedList #Sorting #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
Day 56 - Search a 2D Matrix Approached the problem by leveraging the sorted property of the matrix. Applied binary search over virtual 1D space instead of traversing row-wise. Time Complexity: O(log(m*n)) #Day56 #LeetCode #Java #BinarySearch #Matrix #DSA #CodingPractice #ProblemSolving
To view or add a comment, sign in
-
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