Created
November 14, 2022 11:16
-
-
Save Palakkgoyal/c240b83f043618a3d3ae19958cb23c5e to your computer and use it in GitHub Desktop.
Leetcode question of sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean containsDuplicate(int[] nums) { | |
//first we'll sort our array | |
Arrays.sort(nums); | |
//if any number after sorting is equal to number after it, it means that number has a duplicate, hence, we return true | |
for(int k = 0; k < nums.length - 1; k++){ | |
if(nums[k] == nums[k+1]){ | |
return true; | |
} | |
} | |
//if you don't find any duplicate, return false | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment