Solved LeetCode Medium: Find All Duplicates in an Array with Cyclic Sort

Problem 26 : LeetCode 🚀 LeetCode Medium Solved: Find All Duplicates in an Array (Cyclic Sort Approach) 🧩 I just tackled another LeetCode Medium challenge — this one tested both my problem-solving and in-place algorithm design skills. 🔍 Problem Overview: Given an array of integers where each value is between 1 and n, the goal was to find all duplicate numbers — with O(n) time and O(1) extra space. 💡 My Approach – Cyclic Sort Logic: I used the Cyclic Sort algorithm, which treats the array itself like a hash map. Each element x should be at index x - 1. During the sorting process: If the element is not in the right place → swap it. If a duplicate is detected (arr[i] == arr[arr[i]-1]) → record it. This approach avoids using any extra data structure for indexing and works entirely in-place. ⚙️ Results: ✅ Runtime: 14 ms → Beats 42.80% of Java submissions ✅ Memory: 59.84 MB → Beats 8.98% of submissions ✅ Complexity: O(n) time | O(1) space ✅ Status: Accepted 🎉 (All 29 test cases passed) 📚 Tech Stack Used: Java | Cyclic Sort | HashSet | Array Manipulation | DSA Every solved problem strengthens logic, debugging, and clarity of thinking — a step closer to writing efficient production-grade code. #LeetCode #Java #ProblemSolving #CyclicSort #DataStructures #Algorithms #BackendDeveloper #SpringBoot #LearningInPublic #CodeJourney Question Url : https://lnkd.in/dwz6UWyH

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories