Contains Duplicate Problem Solution: Brute Force and Optimized Approaches

I just recorded myself solving the Contains Duplicate problem and walking through a couple of different approaches. The problem: Given an integer array nums, return true if any value appears more than once. Otherwise, return false. Examples: • [1, 2, 3, 3] → true • [1, 2, 3, 4] → false 💡 Approach 1: Nested Loops (Brute Force) I started with a double loop to compare every pair of elements. ⏱ Time Complexity: O(n²) 📦 Space Complexity: O(1) It works, but it’s not scalable for large inputs. 💡 Approach 2: Sort + Single Pass (Optimized) Then I improved the solution by: 1️⃣ Sorting the array 2️⃣ Looping once to check adjacent elements If nums[i] === nums[i - 1], we’ve found a duplicate. ⏱ Time Complexity: O(n log n) (because of sorting) 📦 Space Complexity: Depends on the sorting implementation I enjoy breaking problems down this way — starting simple, then refining the solution step by step. That’s where real learning happens. Here’s the full walkthrough video: 🔗 https://lnkd.in/efeFYhtC #NeetCode #Algorithms #DataStructures #CodingInterview #JavaScript #SoftwareEngineering #ProblemSolving

To view or add a comment, sign in

Explore content categories