Skip to content

Commit

Permalink
LongestConsecutiveTest - longestConsecutive
Browse files Browse the repository at this point in the history
  • Loading branch information
bky373 committed Jun 6, 2024
1 parent 5e9cc7f commit c451736
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/test/java/LongestConsecutiveTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;

public class LongestConsecutiveTest {

@Test
void longestConsecutive() {
int[] nums = {100, 4, 200, 1, 3, 2};
Set<Integer> numSet = new HashSet<>();
for (int n : nums) {
numSet.add(n);
}
int longest = 0;
for (int n : nums) {
if (numSet.contains(n - 1)) {
continue;
}
int seq = 1;
while (numSet.contains(n + seq)) {
seq++;
}
longest = Math.max(longest, seq);
}
assertThat(longest).isEqualTo(4);
}
}

0 comments on commit c451736

Please sign in to comment.