Leetcode:contains Duplicate-Determine if there are duplicate elements in the array

Source: Internet
Author: User

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  &LT;=&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;&LT;=&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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.