Finding Smallest Common Element in Two Sorted Arrays with HashSet

🚀 Day 89 - #100DaysOfCode Today I solved a problem on finding the smallest common element between two sorted arrays. 🧩 Problem Given two integer arrays, return the first common element between them. If no common element exists, return -1. 💡 Approach I used a HashSet to efficiently check for common elements. Steps: 1️⃣ Insert all elements of the first array into a HashSet. 2️⃣ Traverse the second array. 3️⃣ Check if the current element exists in the set. 4️⃣ If found, return that element immediately. 5️⃣ If no common element is found, return -1. ⏱ Time & Space Complexity Time Complexity: O(n + m) Space Complexity: O(n) 📌 Key Learning: Using a HashSet helps reduce lookup time to O(1), making the solution efficient #DSA #Java #CodingJourney #LeetCode #ProblemSolving #100DaysOfCode

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories