Skip to content

Commit

Permalink
26. Remove Duplicates from Sorted Array
Browse files Browse the repository at this point in the history
  • Loading branch information
Temzor committed May 24, 2024
1 parent c6600a4 commit 2dba3f9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/ru/code/eight/RemoveDuplicates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.code.eight;


public class RemoveDuplicates {
public static int removeDuplicates(int[] a) {
int result = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] != a[result]) {
result++;
a[result] = a[i];
}
}
return result + 1;

}
}
12 changes: 12 additions & 0 deletions src/test/java/ru/code/eight/RemoveDuplicatesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.code.eight;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class RemoveDuplicatesTest {
@Test
void removeDuplicates() {
assertThat(5).isEqualTo(RemoveDuplicates.removeDuplicates(new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}));
}
}

0 comments on commit 2dba3f9

Please sign in to comment.