Skip to content

Commit 2f5cac1

Browse files
author
Karolina Kafel
committed
CodeFights : first duplicate
1 parent 515a2fb commit 2f5cac1

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.other;
2+
3+
class FirstDuplicate {
4+
5+
static int firstDuplicate(int[] a) {
6+
boolean[] duplicates = new boolean[(int) (Math.pow(10, 5)+1)];
7+
int i = 0;
8+
while(i < a.length && !duplicates[a[i]]){
9+
duplicates[a[i++]] = true;
10+
}
11+
return i < a.length && duplicates[a[i]] ? a[i] : -1;
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.other;
2+
3+
import org.junit.Test;
4+
5+
import static com.other.FirstDuplicate.firstDuplicate;
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
public class FirstDuplicateTest {
9+
10+
@Test
11+
public void failingCase13() {
12+
int[] a = new int[]{28, 19, 13, 6, 34, 48, 50, 3, 47, 18, 15, 34, 16, 11, 13, 3, 2, 4, 46, 6, 7, 3, 18, 9, 32, 21, 3, 21, 50, 10, 45, 13, 22, 1, 27, 18, 3, 27, 30, 44, 12, 30, 40, 1, 1, 31, 6, 18, 33, 5};
13+
assertThat(firstDuplicate(a)).isEqualTo(34);
14+
}
15+
}

0 commit comments

Comments
 (0)