1. Title
Contains Duplicate (determine if there are duplicate elements in the array)
2. Address of the topic
https://leetcode.com/problems/contains-duplicate/
3. Topic content
English: 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 ele ment is distinct.
English: Give an array of integers to determine if there are two element values in the array that are the same and return true, otherwise false
4. Method of solving Problems 1
The incoming array is sorted first, the adjacent elements are sorted, and if there is an adjacent element, then at least two elements in the original array are the same.
The Java code is as follows:
import java.util.arrays;/** * @ function Description:leetcode 217 - contains duplicate * @ Developer: tsybius2014 * @ Development Date: October 12, 2015 */public class Solution { /** * See if there are duplicate elements in the array * @param nums * @return */ public boolean containsduplicate (int[] nums) { if (nums.length <=&NBSP;1) { return false; } arrays.sort (nums); for (int i = 1; i < nums.length; i++) { if (nums[i] == nums[i - 1]) { return true; } } return false; }}
5. Method of solving Problems 2
Iterating through an array, placing the values in an array one by HashMap, and traversing to an element that finds that the element value already exists in HashMap, indicates that there are duplicate elements in the original array.
The Java code is as follows:
import java.util.hashmap;/** * @ function Description: leetcode 217 - contains duplicate * @ Developer: tsybius2014 * @ Development Date: October 12, 2015 */public class solution { /** * See if there are duplicate elements in the array * @param nums * @return */ public boolean containsduplicate (int[] nums) { if (nums.length &NBSP;<=&NBSP;1) { return False; } hashmap <Integer, Integer> hashMap = new HashMap<Integer, Integer> (); for (int i = 0; i < nums.length; i++) { if (Hashmap.containskey (nums[i)) { return true; } else { hashmap.put (nums[i], i); } } return false; }}
END
Leetcode:contains Duplicate-Determine if there are duplicate elements in the array