Java solution with two pointers, 4ms @ 98.76%


  • 0

    I used two-pointer technique that is similar to solve mergesort problems. Also, it's discussed in a Leetcode article https://leetcode.com/articles/two-pointer-technique/

    public class Solution {
        public boolean containsDuplicate(int[] nums) {
            if (nums.length <= 1) {
                return false;
            }
    
            Arrays.sort(nums);
    
            int i = 0;
            int j = nums.length -1;
            while (i <= j) {
                if (nums[i] == nums[i+1]) {
                    return true;
                }            
                if (nums[j] == nums[j-1]) {
                    return true;
                }
                i++;
                j--;
            }
            return false;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.