Skip to main content

Command Palette

Search for a command to run...

217. Contains Duplicate

Updated
1 min readView as Markdown

In this problem , we just have to check if the integer array contains duplicates. If it does , return true and if it does not contain any duplicates, return false.

For this, we can use a set.

Definition -a set is a data structure that stores a collection of unique, unordered elements. Unlike lists or arrays, sets automatically prevent duplicate entries and typically use hashing for internal storage, which allows for O(1) average time complexity for membership checks, additions, and deletions.

We declare an empty set, run this array through a loop, adding the elements to the set one by one. Now, if the element cannot be added to the set, that means the array already had that same number before in the array and thus, we can break the loop and return true. And, if the loop gets completed we return false.


Implementation-