Skip to content

Solution - #1 - Nishitha - 10/05/2025 #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

nishithadaryn
Copy link

Problem Explanation

Problem Statement

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Approach

  1. Sort the array so that duplicate elements are adjacent.
  2. Iterate through the sorted array and check if any adjacent elements are equal.

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.

Copy link
Member

@SSexpl SSexpl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a O(NlogN) TC and s O(1) SC , we can have a solution with a better time complexity

@JRS296 JRS296 self-requested a review May 10, 2025 06:09
@JRS296
Copy link
Member

JRS296 commented May 10, 2025

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

Comment on lines +1 to +13
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;
}
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +1 to +12
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;
}
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the solution above. And not following convention. It should be 1 Solution at a time. Follow the Folder structure.

Copy link
Member

@JRS296 JRS296 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes need to be made. Please read the readme.md clearly.

@JRS296
Copy link
Member

JRS296 commented May 10, 2025

Also the title is still not clear. Kindly have a look at the readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants