SprintCode.pro

Подготовка к алгоритмическим задачам

Super

Contains Duplicate

Given an array of integers nums, return true if any value appears more than once in the array, otherwise return false.

Example 1:

Input: nums = [1, 2, 3, 3] Output: true

Example 2:

Input: nums = [1, 2, 3, 4] Output: false

Recommended time and space complexity

You should aim for a solution with O(n) time complexity and O(n) space complexity, where n is the size of the input array.


Hint 1

A brute force solution would be to check every element against every other element in the array. This would be an O(n^2) time complexity solution. Can you think of a more efficient way?


Hint 2

Is it possible to check if an element is a duplicate without comparing it to every other element? Perhaps some data structure would be useful here.


Hint 3

We can use a hash data structure, such as a hash table or hash map, to store elements we've already seen. This will allow us to check if an element is a duplicate in constant time.

This problem tests the ability to efficiently determine the presence of duplicates in an array of integers. It helps develop skills in working with hash tables and sets, and improves understanding of time and space complexity of algorithms. Solving this problem strengthens logical thinking and the ability to optimize code for working with large amounts of data.

Expected Input :

[1, 2, 3, 3]

Expected Output

true