Implement containsDuplicate function to check for duplicates in an array#26
Merged
argondev22 merged 1 commit intomainfrom Dec 2, 2025
Merged
Conversation
| @@ -0,0 +1,18 @@ | |||
| class Solution: | |||
There was a problem hiding this comment.
ファイル名のスペルミス: "dupulicate" は "duplicate" が正しいスペルです。ファイル名を修正することをお勧めします。
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 |
There was a problem hiding this comment.
変数名の問題: dict は Python の組み込み型と同じ名前なので避けるべきです。seen や visited などのより明確な名前を使用することをお勧めします。
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 |
|
|
||
| dict = {} | ||
|
|
||
| # liner scanning |
There was a problem hiding this comment.
コメントのスペルミス: "liner" は "linear" が正しいスペルです。
# linear scanning
Suggested change
| # liner scanning | |
| # linear scanning |
|
|
||
| # liner scanning | ||
| for num in nums: | ||
| # return true if dupulicate is |
There was a problem hiding this comment.
コメントのスペルミスと文法の問題: "dupulicate" は "duplicate" が正しく、また文法も不自然です。
# return true if duplicate is found
Suggested change
| # return true if dupulicate is | |
| # return True if duplicate is found |
Comment on lines
+4
to
+8
| if len(nums) == 1: | ||
| return False | ||
|
|
||
| dict = {} | ||
|
|
There was a problem hiding this comment.
不要なエッジケース処理: 長さ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 = {} |
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 |
There was a problem hiding this comment.
パフォーマンスの改善: 辞書(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Status
[Accepted/Wrong Answer/Time Limit Exceeded]
Runtime
ms
Order
O()
Memory
MB
Order
O()
Time Taken
m s