LeetCode 287 | Find the Duplicate Number | Day 41 | 100 Days LeetCode Challenge | DSA with edSlash

preview_player
Показать описание
👋 Welcome to Day 41 of your transformative 100-Day LeetCode Challenge with edSlash 👋

🔥 Why You Should Watch
Embark on an incredible journey to master Data Structures with today's focused LeetCode problem. Designed for all skill levels, this series is your ultimate guide to cracking coding interviews and becoming a competent programmer.

📚 What We Cover Today
- Find the Duplicate Number | LeetCode Problem Number 287
- Problem-solving techniques
- Walkthrough of today's LeetCode problem

✅ What You'll Gain
- Enhanced understanding of Data Structures
- Improved problem-solving abilities
- Readiness for coding interviews

Remember to share your LeetCode progress on LinkedIn with #DSAwithedSlash and #LeetCode.

To make this challenge exciting we have kept some goodies for our consistent performers who'll tag us on 100 problems for 100 days on linkedIn with #DSAwithedSlash and #LeetCode #edSlash

📌 What's Next
Stay tuned for daily problem-solving insights and expert guidance over the next 100 days. Hit the 'Subscribe' button and ring the bell 🔔 to ensure you don't miss out on future content!

🔗 Important Links

If you find value in our content, please like this video, share it with your friends, and comment below on your experience!

🚀 Let's get started on your journey to mastering Data Structures and Algorithms! 🚀

#DSAwithedSlash #edSlash #leetcode #java #dsa #javaprogramming #coding #datastructures #learncoding
Рекомендации по теме
Комментарии
Автор

I have use the logic of frequency array for this question, this code uses only 3 ms time

class Solution {
public int findDuplicate(int[] nums) {
int[] freq = new int[(int)Math.pow(10, 5)+1];

for(int i =0 ; i<nums.length;i++){
freq[nums[i]]++;

}
for(int i=0;i<nums.length;i++){
if(freq[i]>=2){
return i;
}
}
return -1;
}
}

OmPawar-rj
Автор

I don’t understand y did you use the last for loop for making it positive
It just increases the time complexity

gdriven
Автор

class Solution {
public int findDuplicate(int[] arr){
int i = 0;
while (i < arr.length){
if (arr[i] != i + 1){
int correct = arr[i] - 1;
if (arr[i] != arr[correct]) {
swap(arr, i, correct);
}else {
return arr[i];
}
}else {
i++;
}
}
return -1;
}

static void swap(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}
Alternate Solution.

asmitsingh