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
Post a Comment