Back to Browse

LeetCode 645 | Set Mismatch | Python In-Place Index Marking or Counting | AlgoYogi

18 views
Oct 17, 2025
18:09

Welcome to AlgoYogi! πŸš€ Start Your Smart Coding Prep at πŸ‘‰ https://algoyogi.io We solve LeetCode 645: Set Mismatch using Python. You are given nums from 1 to n where one number is duplicated and one is missing. Return [duplicate, missing]. Approaches: 1) In-place index marking - For each value x in nums, use idx = abs(x) βˆ’ 1 - If nums[idx] is negative, x is the duplicate - Otherwise set nums[idx] = βˆ’nums[idx] - After marking, the index i where nums[i] is positive corresponds to missing = i + 1 2) Counting map (simpler but uses extra space) - Count frequencies; the one with count 2 is duplicate, the one with count 0 is missing πŸ‘‰ Problem Link: https://leetcode.com/problems/set-mismatch/ --- ### ⏱ Timestamps 0:00 Introduction 0:20 Problem Statement 1:00 In-Place Index Marking 14:00 Python Code Walkthrough 16:40 Time and Space Complexity --- ### πŸ’‘ Why Watch? - Classic trick: use the array itself as a hash using signs - O(n) time, O(1) extra space for the in-place method - Easy, interview-friendly reasoning and implementation #LeetCode #SetMismatch #Python #AlgoYogi #Array #InPlace

Download

0 formats

No download links available.

LeetCode 645 | Set Mismatch | Python In-Place Index Marking or Counting | AlgoYogi | NatokHD