Given an array of integers, find if the array contains any duplicates.

Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

Example 1:

Input: [1,2,3,1]
Output: true

Example 2:

Input: [1,2,3,4]
Output: false

Example 3:

Input: [1,1,1,3,3,4,3,2,4,2]
Output: true

Here is my solution:

class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> unique = new HashSet<>(nums.length);

for(int i: nums) {
if(unique.contains(i)) {
return true;
}
unique.add(i);
}

return false;
}
}

You can checkout full explanation by watching below video

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Here is my solution:

class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();

for (int i=0; i<nums.length; i++) {
int difference = target - nums[i];
if (map.containsKey(difference)) {
return new int[] {map.get(difference), i};
}
map.put(nums[i], i);
}
return new int[] {-1, -1};
}
}

You can checkout full explanation by watching below video

Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.

IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots (“.”), e.g.,172.16.254.1;

Besides, leading zeros in the IPv4…

Rajdeep Kaur

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store