Skip to content

Conversation

@syoshida20
Copy link
Owner

@syoshida20 syoshida20 commented Jun 8, 2025

問題URL

https://leetcode.com/problems/contains-duplicate/description/

問題文

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

Example 1:

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

The element 1 occurs at the indices 0 and 3.

Example 2:

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

All elements are distinct.

Example 3:

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

Constraints:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109

@syoshida20 syoshida20 marked this pull request as ready for review June 22, 2025 09:22
const containsDuplicate = function(nums) {
const numToCount = new Map()
for (const num of nums) {
if (numToCount.get(num) !== undefined) {
Copy link

Choose a reason for hiding this comment

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

numの存在判定をしているので、 Map.prototype.has() を用いる方が自然だと感じました。
あと今回のケースでは問題になりませんが、numToCountに undefined を入れるような場合値が登録されているのかどうか判別できませんね。

$ node
Welcome to Node.js v22.16.0.
Type ".help" for more information.
> var m = new Map();
undefined
> m.set(1, undefined)
Map(1) { 1 => undefined }
> m.get(1)
undefined
> m.get(2)
undefined
> m.has(1)
true
> m.has(2)
false

そもそも存在しているかしか見る必要がないので、Setを用いる方が自然な気はしますが。

Copy link
Owner Author

@syoshida20 syoshida20 Jun 22, 2025

Choose a reason for hiding this comment

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

Map.prototype.has() を用いる方が自然だと感じました。

確かに、set関数でundefined以外の値(boolean)を設定することを前提としたコードになっていますね。
ご指摘の通りだと思いました。

そもそも存在しているかしか見る必要がないので、Setを用いる方が自然な気はしますが。

同じ感想を持ちました!

Choose a reason for hiding this comment

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

私も、出現回数を保持する意図がなく、出現したかどうかだけを見ているので、Setの解法が一番最初に思いつきやすい・読む人にも意図が伝わりやすいかなと思いました!

const containsDuplicate = function(nums) {
const numToCount = new Map()
for (const num of nums) {
if (numToCount.get(num) !== undefined) {

Choose a reason for hiding this comment

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

私も、出現回数を保持する意図がなく、出現したかどうかだけを見ているので、Setの解法が一番最初に思いつきやすい・読む人にも意図が伝わりやすいかなと思いました!

* `*2`
* 時間計算量: O(N log N)
* 空間計算量: O(1)
* Javasciprtにおいては、sort関数はin-placeで行われるため、O(1)の空間計算量で、sortをできる。

Choose a reason for hiding this comment

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

JavaScriptに明るくなく、軽く調べただけの知識で恐縮ですが、JavaScriptにおいてのsort()はブラウザの実装によって異なるため、時間・空間計算量はJavaScriptという括りでは括れなさそうな気がします。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

The time and space complexity of the sort cannot be guaranteed as it depends on the implementation.

はっきりとしたReferenceは軽く調べた限りでは手に入れられなかったのですが、最近のChromeなどはTimsortを使用しているらしく、空間計算量O(1)を保証できないような気がします。


```javascript
const containsDuplicate = function(nums) {
for (let i=0; i < nums.length; i++) {
Copy link

Choose a reason for hiding this comment

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

二項演算子の両側にスペースを空けることをおすすめします。

https://google.github.io/styleguide/jsguide.html#formatting-horizontal-whitespace

On both sides of any binary or ternary operator.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Google Style Guide でスペースについて書いている記載と場所を把握できておりませんでした。

教えてくださり、ありがとうございます。


```javascript
const containsDuplicate = function(nums) {
const numToCount = new Map()
Copy link

Choose a reason for hiding this comment

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

各文のあとにセミコロンを書くことをおすすめします。

参考までにスタイルガイドへのリンクを貼ります。

https://google.github.io/styleguide/jsguide.html#formatting-semicolons-are-required

Every statement must be terminated with a semicolon.

ただし、上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。

Copy link
Owner Author

Choose a reason for hiding this comment

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

こちらもスタイルガイドにおいて推奨されていることを把握できておりませんでした。
ありがとうございます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants