[LeetCode-interview algorithm classic-Java implementation] [217-Contains Duplicate (including Duplicate elements)], leetcode -- java

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [217-Contains Duplicate (including Duplicate elements)], leetcode -- java
[217-Contains Duplicate (including Duplicate elements )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Download the Code [https://github.com/wang-jun-chao]Original question

Given an array of integers, find if the array contains any duplicates. your function shoshould return true if any value appears at least twice in the array, and it shoshould return false if every element is distinct.

Theme

Returns an integer array to determine whether the array contains repeated elements. If any number in the array appears at least twice, your function should return true. If each element is unique, false is returned.

Solutions

Use set Data Structure

Code Implementation

Algorithm Implementation class

Import java. util. hashSet; import java. util. set; public class Solution {public boolean containsDuplicate (int [] nums) {// if (nums! = Null & nums. length> 1) {// create a hashSet <Integer> Set = new HashSet <> (nums. length); for (int I: nums) {// returns true if (set. contains (I) {return true;} // Add it to else {set. add (I) ;}}return false ;}}
Evaluation Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.

Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/48046275]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.