Not a member of GistPad yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- /** T(N) = O(N), O(N) */
- public boolean hasDuplicate(int[] nums) {
- Set<Integer> set = new HashSet<>();
- for(int ele : nums)
- set.add(ele);
- // set size won't be same if array has duplicates, because in set we cannot store duplicate elements
- if(set.size() != nums.length)
- return true;
- return false;
- }
- /** T(N) = O(N*Log N), O(1) */
- public boolean hasDuplicate(int[] nums) {
- for(int i=1; i<nums.length; i++)
- {
- if(nums[i-1] == nums[i])
- return true;
- }
- return false;
- }
- /** T(N) = O(N), O(1) */
- // only if array has positive integers
- public boolean hasDuplicate(int[] nums)
- {
- for(int i=0; i<nums.length-1; i++)
- {
- if(diff == 1)
- continue;
- else
- return true;
- }
- return false;
- }
- }
RAW Paste Data
Copied
