forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/eugenp/tutorials into swa…
…gger-spring-sec-3
- Loading branch information
Showing
931 changed files
with
69,626 additions
and
2,288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>algorithms-miscellaneous-9</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>algorithms-miscellaneous-9</name> | ||
|
||
<parent> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>algorithms-modules</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
</project> |
41 changes: 41 additions & 0 deletions
41
...miscellaneous-10/src/main/java/com/baeldung/algorithms/firstduplicate/FirstDuplicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.baeldung.algorithms.firstduplicate; | ||
|
||
import java.util.HashSet; | ||
|
||
public class FirstDuplicate { | ||
|
||
public int firstDuplicateBruteForce(int[] arr) { | ||
int minIndex = arr.length; | ||
for (int i = 0; i < arr.length - 1; i++) { | ||
for (int j = i + 1; j < arr.length; j++) { | ||
if (arr[i] == arr[j]) { | ||
minIndex = Math.min(minIndex, j); | ||
break; | ||
} | ||
} | ||
} | ||
return minIndex == arr.length ? -1 : minIndex; | ||
} | ||
|
||
public int firstDuplicateHashSet(int[] arr) { | ||
HashSet<Integer> firstDuplicateSet = new HashSet<>(); | ||
for (int i = 0; i < arr.length; i++) { | ||
if (firstDuplicateSet.contains(arr[i])) { | ||
return i; | ||
} | ||
firstDuplicateSet.add(arr[i]); | ||
} | ||
return -1; | ||
} | ||
|
||
public int firstDuplicateArrayIndexing(int[] arr) { | ||
for (int i = 0; i < arr.length; i++) { | ||
int val = Math.abs(arr[i]) - 1; | ||
if (arr[val] < 0) { | ||
return i; | ||
} | ||
arr[val] = -arr[val]; | ||
} | ||
return -1; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...neous-10/src/test/java/com/baeldung/algorithms/firstduplicate/FirstDuplicateUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.baeldung.algorithms.firstduplicate; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class FirstDuplicateUnitTest { | ||
|
||
@Test | ||
public void givenArray_whenUsingBruteForce_thenFindFirstDuplicate() { | ||
FirstDuplicate firstDuplicate = new FirstDuplicate(); | ||
|
||
assertEquals(4, firstDuplicate.firstDuplicateBruteForce(new int[] { 2, 1, 3, 5, 3, 2 })); | ||
assertEquals(-1, firstDuplicate.firstDuplicateBruteForce(new int[] { 1, 2, 3, 4, 5 })); | ||
assertEquals(2, firstDuplicate.firstDuplicateBruteForce(new int[] { 2, 1, 1, 2 })); | ||
assertEquals(1, firstDuplicate.firstDuplicateBruteForce(new int[] { 1, 1 })); | ||
assertEquals(-1, firstDuplicate.firstDuplicateBruteForce(new int[] {})); | ||
} | ||
|
||
@Test | ||
public void givenArray_whenUsingHashSet_thenFindFirstDuplicate() { | ||
FirstDuplicate firstDuplicate = new FirstDuplicate(); | ||
|
||
assertEquals(4, firstDuplicate.firstDuplicateHashSet(new int[] { 2, 1, 3, 5, 3, 2 })); | ||
assertEquals(-1, firstDuplicate.firstDuplicateHashSet(new int[] { 1, 2, 3, 4, 5 })); | ||
assertEquals(2, firstDuplicate.firstDuplicateHashSet(new int[] { 2, 1, 1, 2 })); | ||
assertEquals(1, firstDuplicate.firstDuplicateHashSet(new int[] { 1, 1 })); | ||
assertEquals(-1, firstDuplicate.firstDuplicateHashSet(new int[] {})); | ||
} | ||
|
||
@Test | ||
public void givenArray_whenUsingArrayIndexing_thenFindFirstDuplicate() { | ||
FirstDuplicate firstDuplicate = new FirstDuplicate(); | ||
|
||
assertEquals(4, firstDuplicate.firstDuplicateArrayIndexing(new int[] { 2, 1, 3, 5, 3, 2 })); | ||
assertEquals(-1, firstDuplicate.firstDuplicateArrayIndexing(new int[] { 1, 2, 3, 4, 5 })); | ||
assertEquals(2, firstDuplicate.firstDuplicateArrayIndexing(new int[] { 2, 1, 1, 2 })); | ||
assertEquals(1, firstDuplicate.firstDuplicateArrayIndexing(new int[] { 1, 1 })); | ||
assertEquals(-1, firstDuplicate.firstDuplicateArrayIndexing(new int[] {})); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
### Relevant Articles: | ||
|
||
- [Sorting a String Alphabetically in Java](https://www.baeldung.com/java-sort-string-alphabetically) | ||
- [Sorting Strings by Contained Numbers in Java](https://www.baeldung.com/java-sort-strings-contained-numbers) | ||
- [Guide to In-Place Sorting Algorithm Works with a Java Implementation](https://www.baeldung.com/java-in-place-sorting) | ||
- [Partitioning and Sorting Arrays with Many Repeated Entries with Java Examples](https://www.baeldung.com/java-sorting-arrays-with-repeated-entries) | ||
- [Gravity/Bead Sort in Java](https://www.baeldung.com/java-gravity-bead-sort) | ||
- More articles: [[<-- prev]](/algorithms-sorting) | ||
- [Selection Sort in Java](https://www.baeldung.com/java-selection-sort) | ||
- [Bubble Sort in Java](https://www.baeldung.com/java-bubble-sort) | ||
- [Insertion Sort in Java](https://www.baeldung.com/java-insertion-sort) | ||
- [Heap Sort in Java](https://www.baeldung.com/java-heap-sort) | ||
- [Counting Sort in Java](https://www.baeldung.com/java-counting-sort) | ||
- [Radix Sort in Java](https://www.baeldung.com/java-radix-sort) | ||
- More articles: [[<-- prev]](/algorithms-modules/algorithms-sorting) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.