Simple solution with sorting in Java


  • 0
    T
    public class Solution {
        public boolean containsDuplicate(int[] nums) {
            int n = nums.length;
            if(n == 0 || n == 1) return false;
            Arrays.sort(nums);
            int i = 0;
            int j = 1;
            while(j<n){
                if(nums[i]==nums[j]) 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.