🚀 LeetCode 128: Longest Consecutive Sequence | O(n) HashSet Solution | Python | DSA Day 20 🔥

🚀 LeetCode 128: Longest Consecutive Sequence | HashSet | O(n) Optimal Solution | Python | DSA Day 20 🔥 Day 20 of my DSA in Python Series! In this video, we solve LeetCode 128 – Longest Consecutive Sequence using the optimal HashSet approach that runs in O(n) time. Many beginners first think of sorting the array, but interviews expect an O(n) solution. In this video, you'll learn the intuition behind using a HashSet to find the longest consecutive sequence efficiently. 🐢 Approach 1: Sorting (Brute Force) class Solution: def longestConsecutive(self, nums): if not nums: return 0 nums.sort() longest = 1 current = 1 for i in range(1, len(nums)): if nums[i] == nums[i - 1]: continue elif nums[i] == nums[i - 1] + 1: current += 1 else: longest = max(longest, current) current = 1 return max(longest, current) ⏱ Time Complexity: O(n log n) 📦 Space Complexity: O(1) (excluding sorting space) ⚡ Approach 2: HashSet (Optimal) class Solution: def longestConsecutive(self, nums): num_set = set(nums) longest = 0 for num in num_set: if num - 1 not in num_set: current = num length = 1 while current + 1 in num_set: current += 1 length += 1 longest = max(longest, length) return longest ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 📚 What You'll Learn ✅ Longest Consecutive Sequence Explained ✅ HashSet in Python ✅ Why we check num - 1 ✅ Start of Sequence Concept ✅ Sorting vs HashSet Approach ✅ Interview Optimization ✅ Time & Space Complexity Analysis ✅ Python DSA Interview Questions If this video helps you, please Like 👍, Share 🚀, Comment 💬, and Subscribe 🔔 Bharath Builds for more Python DSA, Coding Interview, and Placement Preparation content. 📌 Hashtags #LeetCode128 #LongestConsecutiveSequence #HashSet #Python #PythonDSA #DSA #CodingInterview #Algorithms #DataStructures #InterviewPreparation #PythonProgramming #LeetCode #Programmer #bharathbuilds 🎥 DSA in Python Playlist    • DSA in Python ಕನ್ನಡದಲ್ಲಿ | Top 100 LeetCod...   💻 LeetCode Profile https://leetcode.com/u/BHARATH_BHUVI/ ⭐ GitHub https://github.com/Bharathkrishnamurthy 💼 LinkedIn   / bharathk5052