Array Containing Duplicates

public class ArrayContainsDuplicate {

    public static void main(String args[])
    {
        int[] nums = {1,2,3,1};
        boolean returnValue = containsDuplicate(nums);

        System.out.println("returnValue-->"+returnValue);
    }

    public static boolean containsDuplicate(int[] nums) {

        if(nums.length==1) return false;
        HashSet<Integer> hashSet = new HashSet<>();

        for(int i =0; i<nums.length;i++)
        {
            if(!hashSet.add(nums[i]))return true;
        }

        return false;
    }
}

Comments

Popular posts from this blog

Java 8 : Find the number starts with 1 from a list of integers

Optional Vs Null Check

Important Linux Commands