Adonais's notebook


  • Home

  • About

  • Tags

  • Categories

  • Archives

  • Search

[Leetcode] Set

Posted on 2018-11-19 | In Leetcode , Set |

287. Find the Duplicate Number

Leetcode: https://leetcode.com/problems/find-the-duplicate-number/solution/

Solution 1. Using a set to detect duplicates

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int findDuplicate(int[] nums) {
Set<Integer> seen = new HashSet<Integer>();
for (int num : nums) {
if (seen.contains(num)) {
return num;
}
seen.add(num);
}

return -1;
}
}
# leetcode # set
[Leetcode] Heaps & Priority Queue
[Leetcode] Graph
  • Table of Contents
  • Overview

Adonais0

140 posts
131 categories
112 tags
GitHub Twitter
  1. 1. 287. Find the Duplicate Number
© 2024 Adonais0
Powered by Hexo
|
Theme — NexT.Mist v7.8.0