"Leetcode-Interview algorithm classic-java Implementation" "217-contains Duplicate (contains duplicate elements)"

Source: Internet
Author: User

"217-contains Duplicate (contains repeating elements)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" code Download "Https://github.com/Wang-Jun-Chao" Original Question

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.

Main Topic

Given an array of integers, determine whether the array contains duplicate elements. If any number in the array appears at least two times, your function should return true if each element is unique and return false.

Thinking of solving problems

Using the Set data structure

Code Implementation

Algorithm implementation class

Import Java.util.hashset;import Java.util.Set; Public classSolution { PublicBooleancontainsduplicate(int[] nums) {the number of elements is greater than 1 to do the following        if(Nums! =NULL&& nums.length >1) {//Create a HashSetSet<integer>Set=NewHashset<> (nums.length); for(intI:nums) {//Returns True if the element already exists                if(Set. Contains (i)) {return true; }//Not added to the element collection                Else{Set. Add (i); }            }        }return false; }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/48046275"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "217-contains Duplicate (contains duplicate elements)"

Related Article

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.