Skip to content

Implement containsDuplicate function to check for duplicates in an array#26

Merged
argondev22 merged 1 commit intomainfrom
20251203/24-217-contains-duplicate
Dec 2, 2025
Merged

Implement containsDuplicate function to check for duplicates in an array#26
argondev22 merged 1 commit intomainfrom
20251203/24-217-contains-duplicate

Conversation

@argondev22
Copy link
Copy Markdown
Owner

Status

[Accepted/Wrong Answer/Time Limit Exceeded]

Runtime

ms

Order

O()

Memory

MB

Order

O()

Time Taken

m s

@argondev22 argondev22 requested a review from Copilot December 2, 2025 23:24
@argondev22 argondev22 self-assigned this Dec 2, 2025
@argondev22 argondev22 linked an issue Dec 2, 2025 that may be closed by this pull request
@argondev22 argondev22 merged commit ba3bf57 into main Dec 2, 2025
4 checks passed
@argondev22 argondev22 deleted the 20251203/24-217-contains-duplicate branch December 2, 2025 23:25
@github-project-automation github-project-automation Bot moved this from Todo to Done in My Learnings Dec 2, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

LeetCode 問題 #217 (Contains Duplicate) の実装を追加するプルリクエストです。配列内に重複する要素が存在するかをチェックする関数を実装しています。

主な変更点:

  • ハッシュテーブル(辞書)を使用した O(n) の時間計算量での重複検出アルゴリズムの実装
  • エッジケース(長さ1の配列)の処理を追加
  • 線形スキャンによる効率的な重複チェック

@@ -0,0 +1,18 @@
class Solution:
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ファイル名のスペルミス: "dupulicate" は "duplicate" が正しいスペルです。ファイル名を修正することをお勧めします。

Copilot uses AI. Check for mistakes.
Comment on lines +7 to +15
dict = {}

# liner scanning
for num in nums:
# return true if dupulicate is
if num in dict:
return True

dict[num] = True
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

変数名の問題: dict は Python の組み込み型と同じ名前なので避けるべきです。seenvisited などのより明確な名前を使用することをお勧めします。

seen = {}
# または
seen = set()  # よりメモリ効率的
Suggested change
dict = {}
# liner scanning
for num in nums:
# return true if dupulicate is
if num in dict:
return True
dict[num] = True
seen = {}
# liner scanning
for num in nums:
# return true if dupulicate is
if num in seen:
return True
seen[num] = True

Copilot uses AI. Check for mistakes.

dict = {}

# liner scanning
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントのスペルミス: "liner" は "linear" が正しいスペルです。

# linear scanning
Suggested change
# liner scanning
# linear scanning

Copilot uses AI. Check for mistakes.

# liner scanning
for num in nums:
# return true if dupulicate is
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントのスペルミスと文法の問題: "dupulicate" は "duplicate" が正しく、また文法も不自然です。

# return true if duplicate is found
Suggested change
# return true if dupulicate is
# return True if duplicate is found

Copilot uses AI. Check for mistakes.
Comment on lines +4 to +8
if len(nums) == 1:
return False

dict = {}

Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要なエッジケース処理: 長さ1の配列のチェックは不要です。空の辞書/セットで同じロジックが正しく動作します。このチェックを削除することでコードがよりシンプルになります。

def containsDuplicate(self, nums: List[int]) -> bool:
    seen = set()
    for num in nums:
        if num in seen:
            return True
        seen.add(num)
    return False
Suggested change
if len(nums) == 1:
return False
dict = {}
dict = {}

Copilot uses AI. Check for mistakes.
Comment on lines +7 to +15
dict = {}

# liner scanning
for num in nums:
# return true if dupulicate is
if num in dict:
return True

dict[num] = True
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

パフォーマンスの改善: 辞書(dict)の代わりに set を使用することをお勧めします。値を保存する必要がなく、メモリ効率が良くなります。

現在の実装:

dict = {}
# ...
dict[num] = True

改善案:

seen = set()
# ...
seen.add(num)

set を使うとメモリ使用量が削減され、意図がより明確になります。

Suggested change
dict = {}
# liner scanning
for num in nums:
# return true if dupulicate is
if num in dict:
return True
dict[num] = True
seen = set()
# liner scanning
for num in nums:
# return true if dupulicate is
if num in seen:
return True
seen.add(num)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

217. Contains Duplicate

2 participants