Skip to content

169. Majority Element (from Grind 75 questions) Easy#4

Merged
erutako merged 6 commits intomainfrom
majorityElement
Jun 17, 2024
Merged

169. Majority Element (from Grind 75 questions) Easy#4
erutako merged 6 commits intomainfrom
majorityElement

Conversation

@erutako
Copy link
Owner

@erutako erutako commented Jun 7, 2024

continue;
}

return num;
Copy link

Choose a reason for hiding this comment

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

私はループの最後が return なのに抵抗感があり、continue と入れ替えますね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

小田さん、コメントありがとうございます!
確かに自分もそのように感じるかもしれないです。

特定の条件下でループ内の後続処理をスキップしてループを継続したいのか、ループをストップして値を返したいのか、どちらを強調するかが使い分けする際の判断軸になると考えると、今回のケースでは過半数であることがわかった時点で値を返したいので、後者を強調すべきで、その場合はreturnが先のほうがよいということなのかな?と感じましたmm

@erutako erutako merged commit 364a521 into main Jun 17, 2024
@erutako erutako deleted the majorityElement branch July 20, 2024 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants