Skip to content

Commit 6a46b66

Browse files
authored
Merge branch 'eugenp:master' into spring-web-test1
2 parents 2421dfe + 076885d commit 6a46b66

File tree

1,146 files changed

+73314
-2782
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,146 files changed

+73314
-2782
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ persistence-modules/neo4j/data/**
144144
/libraries-io/src/main/resources/application4.csv
145145
/persistence-modules/hibernate6/finalRecordingTestMvn.jfr
146146
/core-java-modules/core-java-io-conversions-3/src/test/resources/xlsxToCsv_output.csv
147+
/core-java-modules/core-java-io-5/output.txt
148+
/core-java-modules/core-java-io-apis-2/sample.txt
147149
/persistence-modules/core-java-persistence-3/test.mv.db
148-
/apache-libraries/src/main/java/com/baeldung/avro/
150+
/apache-libraries/src/main/java/com/baeldung/apache/avro/
151+
/apache-libraries-2/cars.avro
149152

150153
#spring-cloud
151154
/spring-cloud-modules/spring-cloud-bootstrap/gateway/src/main/resources/static/home/3rdpartylicenses.txt

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,19 @@ We also have a parents profile to build only parent modules.
4444

4545
Therefore, we have a total of 9 profiles:
4646

47-
| Profile | Includes | Type of test enabled |
48-
|-------------------|-----------------------------| -------------------- |
49-
| default | JDK21 projects | *UnitTest |
50-
| integration | JDK21 projects | *IntegrationTest |
51-
| default-jdk17 | JDK17 and above projects | *UnitTest |
52-
| integration-jdk17 | JDK17 and above projects | *IntegrationTest |
53-
| default-heavy | Heavy/long running projects | *UnitTest |
54-
| integration-heavy | Heavy/long running projects | *IntegrationTest |
55-
| default-jdk8 | JDK8 projects | *UnitTest |
56-
| integration-jdk8 | JDK8 projects | *IntegrationTest |
57-
| parents | Set of parent modules | None |
47+
| Profile | Includes | Type of test enabled |
48+
|--------------------|-----------------------------|------------------------------|
49+
| default | JDK21 projects | *UnitTest |
50+
| integration | JDK21 projects | *IntegrationTest |
51+
| default-jdk17 | JDK17 and above projects | *UnitTest |
52+
| integration-jdk17 | JDK17 and above projects | *IntegrationTest |
53+
| profile-jdk22 | JDK22 projects | *UnitTest & *IntegrationTest |
54+
| profile-jdk23 | JDK23 projects | *UnitTest & *IntegrationTest |
55+
| default-heavy | Heavy/long running projects | *UnitTest |
56+
| integration-heavy | Heavy/long running projects | *IntegrationTest |
57+
| default-jdk8 | JDK8 projects | *UnitTest |
58+
| integration-jdk8 | JDK8 projects | *IntegrationTest |
59+
| parents | Set of parent modules | None |
5860

5961
Building the project
6062
====================
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>algorithms-miscellaneous-9</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>algorithms-miscellaneous-9</name>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>algorithms-modules</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
</parent>
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.algorithms.firstduplicate;
2+
3+
import java.util.HashSet;
4+
5+
public class FirstDuplicate {
6+
7+
public int firstDuplicateBruteForce(int[] arr) {
8+
int minIndex = arr.length;
9+
for (int i = 0; i < arr.length - 1; i++) {
10+
for (int j = i + 1; j < arr.length; j++) {
11+
if (arr[i] == arr[j]) {
12+
minIndex = Math.min(minIndex, j);
13+
break;
14+
}
15+
}
16+
}
17+
return minIndex == arr.length ? -1 : minIndex;
18+
}
19+
20+
public int firstDuplicateHashSet(int[] arr) {
21+
HashSet<Integer> firstDuplicateSet = new HashSet<>();
22+
for (int i = 0; i < arr.length; i++) {
23+
if (firstDuplicateSet.contains(arr[i])) {
24+
return i;
25+
}
26+
firstDuplicateSet.add(arr[i]);
27+
}
28+
return -1;
29+
}
30+
31+
public int firstDuplicateArrayIndexing(int[] arr) {
32+
for (int i = 0; i < arr.length; i++) {
33+
int val = Math.abs(arr[i]) - 1;
34+
if (arr[val] < 0) {
35+
return i;
36+
}
37+
arr[val] = -arr[val];
38+
}
39+
return -1;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.algorithms.firstduplicate;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
public class FirstDuplicateUnitTest {
8+
9+
@Test
10+
public void givenArray_whenUsingBruteForce_thenFindFirstDuplicate() {
11+
FirstDuplicate firstDuplicate = new FirstDuplicate();
12+
13+
assertEquals(4, firstDuplicate.firstDuplicateBruteForce(new int[] { 2, 1, 3, 5, 3, 2 }));
14+
assertEquals(-1, firstDuplicate.firstDuplicateBruteForce(new int[] { 1, 2, 3, 4, 5 }));
15+
assertEquals(2, firstDuplicate.firstDuplicateBruteForce(new int[] { 2, 1, 1, 2 }));
16+
assertEquals(1, firstDuplicate.firstDuplicateBruteForce(new int[] { 1, 1 }));
17+
assertEquals(-1, firstDuplicate.firstDuplicateBruteForce(new int[] {}));
18+
}
19+
20+
@Test
21+
public void givenArray_whenUsingHashSet_thenFindFirstDuplicate() {
22+
FirstDuplicate firstDuplicate = new FirstDuplicate();
23+
24+
assertEquals(4, firstDuplicate.firstDuplicateHashSet(new int[] { 2, 1, 3, 5, 3, 2 }));
25+
assertEquals(-1, firstDuplicate.firstDuplicateHashSet(new int[] { 1, 2, 3, 4, 5 }));
26+
assertEquals(2, firstDuplicate.firstDuplicateHashSet(new int[] { 2, 1, 1, 2 }));
27+
assertEquals(1, firstDuplicate.firstDuplicateHashSet(new int[] { 1, 1 }));
28+
assertEquals(-1, firstDuplicate.firstDuplicateHashSet(new int[] {}));
29+
}
30+
31+
@Test
32+
public void givenArray_whenUsingArrayIndexing_thenFindFirstDuplicate() {
33+
FirstDuplicate firstDuplicate = new FirstDuplicate();
34+
35+
assertEquals(4, firstDuplicate.firstDuplicateArrayIndexing(new int[] { 2, 1, 3, 5, 3, 2 }));
36+
assertEquals(-1, firstDuplicate.firstDuplicateArrayIndexing(new int[] { 1, 2, 3, 4, 5 }));
37+
assertEquals(2, firstDuplicate.firstDuplicateArrayIndexing(new int[] { 2, 1, 1, 2 }));
38+
assertEquals(1, firstDuplicate.firstDuplicateArrayIndexing(new int[] { 1, 1 }));
39+
assertEquals(-1, firstDuplicate.firstDuplicateArrayIndexing(new int[] {}));
40+
}
41+
}
42+
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
### Relevant Articles:
22

3-
- [Sorting a String Alphabetically in Java](https://www.baeldung.com/java-sort-string-alphabetically)
43
- [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers)
54
- [Guide to In-Place Sorting Algorithm Works with a Java Implementation](https://www.baeldung.com/java-in-place-sorting)
65
- [Partitioning and Sorting Arrays with Many Repeated Entries with Java Examples](https://www.baeldung.com/java-sorting-arrays-with-repeated-entries)
76
- [Gravity/Bead Sort in Java](https://www.baeldung.com/java-gravity-bead-sort)
8-
- More articles: [[<-- prev]](/algorithms-sorting)
7+
- [Selection Sort in Java](https://www.baeldung.com/java-selection-sort)
8+
- [Bubble Sort in Java](https://www.baeldung.com/java-bubble-sort)
9+
- [Insertion Sort in Java](https://www.baeldung.com/java-insertion-sort)
10+
- [Heap Sort in Java](https://www.baeldung.com/java-heap-sort)
11+
- [Counting Sort in Java](https://www.baeldung.com/java-counting-sort)
12+
- [Radix Sort in Java](https://www.baeldung.com/java-radix-sort)
13+
- More articles: [[<-- prev]](/algorithms-modules/algorithms-sorting)

0 commit comments

Comments
 (0)