Solution - #1 - Nishitha - 10/05/2025#49
Solution - #1 - Nishitha - 10/05/2025#49nishithadaryn wants to merge 2 commits intoDijkstra-Edu:masterfrom
Conversation
SSexpl
left a comment
There was a problem hiding this comment.
this is a O(NlogN) TC and s O(1) SC , we can have a solution with a better time complexity
You can add multiple solutions, as well as their explanations. The idea is to start from a not so optimized solution, and gradually move to an optimized solution. You can follow this for reference: https://github.com/Dijkstra-Edu/LeetCode-Solutions/pull/3/files |
| class Solution { | ||
| public: | ||
| bool containsDuplicate(vector<int>& nums) { | ||
| sort(nums.begin(), nums.end()); // O(n log n) | ||
| for (int i = 1; i < nums.size(); ++i) { | ||
| if (nums[i] == nums[i - 1]) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| }; | ||
|
|
There was a problem hiding this comment.
- The File name is not following convention.
- There is no explanation.md file. Kindly read it and have a clear understanding on how to proceed. You can use the template file: https://github.com/Dijkstra-Edu/LeetCode-Solutions/blob/master/Explanation-Template.md
- Also use these merged PR's for reference: https://github.com/Dijkstra-Edu/LeetCode-Solutions/pulls?q=is%3Apr+is%3Aclosed
| class Solution { | ||
| public: | ||
| bool containsDuplicate(vector<int>& nums) { | ||
| sort(nums.begin(), nums.end()); // O(n log n) | ||
| for (int i = 1; i < nums.size(); ++i) { | ||
| if (nums[i] == nums[i - 1]) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| }; |
There was a problem hiding this comment.
Same as the solution above. And not following convention. It should be 1 Solution at a time. Follow the Folder structure.
JRS296
left a comment
There was a problem hiding this comment.
Changes need to be made. Please read the readme.md clearly.
|
Also the title is still not clear. Kindly have a look at the readme. |
Problem Explanation
Problem Statement
Given an integer array
nums, returntrueif any value appears at least twice in the array, and returnfalseif every element is distinct.Approach
Code Explanation
sort(nums.begin(), nums.end());: Sorts the array in ascending order.for (int i = 1; i < nums.size(); ++i): Iterates through the array starting from the second element.if (nums[i] == nums[i - 1]): Checks if the current element is equal to the previous one.return true;: Returns true immediately upon finding a duplicate.return false;: If no duplicates are found after the loop, returns false.