"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)"