Skip to content

Files

26 lines (20 loc) · 804 Bytes

File metadata and controls

26 lines (20 loc) · 804 Bytes

Contains Duplicate

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 element is distinct.

Solution

直接使用hashtable即可

bool containsDuplicate(vector<int> &nums) {
	unordered_set<int> set;
	for (int i : nums) {
		if (set.find(i) != set.end())
			return true;
		else
			set.insert(i);
	}
	return false;
}

扩展