ভিডিওতে দেখো — YouTube @algonur_com
কোড
Python
def containsDuplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))Java
public boolean containsDuplicate(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int n : nums)
if (!seen.add(n)) return true;
return false;
}Kotlin
fun containsDuplicate(nums: IntArray): Boolean =
nums.size != nums.toHashSet().sizeC++
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> seen;
for (int n : nums)
if (!seen.insert(n).second) return true;
return false;
}C
bool containsDuplicate(int* nums, int numsSize) {
for (int i = 0; i < numsSize; i++)
for (int j = i + 1; j < numsSize; j++)
if (nums[i] == nums[j]) return true;
return false;
}জটিলতা
O(n)
সময় — অ্যারেটা একবারই ঘুরি, ম্যাপে খোঁজা O(1)।
O(n)
জায়গা — সবচেয়ে খারাপ ক্ষেত্রে সব সংখ্যা ম্যাপে থাকে।
আগের সমস্যা—পরের সমস্যাValid Anagram →