Bael 7510 (#15776)
* BAEL-7490 read write file in separate thread * Change the to try resources * Update the code to sync with article * First draft * Change module * Update the method return type to Integer and return null if not found * Remove public as using junit 5 --------- Co-authored-by: Wynn Teo <wynnteo@Wynns-MacBook-Pro.local>
This commit is contained in:
parent
6ada5de93e
commit
35ef9393c1
@ -0,0 +1,61 @@
|
|||||||
|
package com.baeldung.algorithms.firstnonrepeating;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class FirstNonRepeatingElement {
|
||||||
|
public static Integer findFirstNonRepeatingUsingForLoop(List<Integer> list) {
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
int current = list.get(i);
|
||||||
|
boolean isRepeating = false;
|
||||||
|
for (int j = 0; j < list.size(); j++) {
|
||||||
|
if (i != j && current == list.get(j)) {
|
||||||
|
isRepeating = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isRepeating) {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer findFirstNonRepeatedElementUsingIndex(List<Integer> list) {
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
if (list.indexOf(list.get(i)) == list.lastIndexOf(list.get(i))) {
|
||||||
|
return list.get(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer findFirstNonRepeatingUsingHashMap(List<Integer> list) {
|
||||||
|
Map<Integer, Integer> counts = new HashMap<>();
|
||||||
|
for (int num : list) {
|
||||||
|
counts.put(num, counts.getOrDefault(num, 0) + 1);
|
||||||
|
}
|
||||||
|
for (int num : list) {
|
||||||
|
if (counts.get(num) == 1) {
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer findFirstNonRepeatingUsingArray(List<Integer> list) {
|
||||||
|
int maxElement = Collections.max(list);
|
||||||
|
int[] frequency = new int[maxElement + 1];
|
||||||
|
for (int num : list) {
|
||||||
|
frequency[num]++;
|
||||||
|
}
|
||||||
|
for (int num : list) {
|
||||||
|
if (frequency[num] == 1) {
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package com.baeldung.algorithms.firstnonrepeating;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class FirstNonRepeatingElementUnitTest {
|
||||||
|
|
||||||
|
private List<Integer> list;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
list = Arrays.asList(1, 2, 3, 2, 1, 4, 5, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingForLoop_thenReturnFirstNonRepeatingElement() {
|
||||||
|
int result = FirstNonRepeatingElement.findFirstNonRepeatingUsingForLoop(list);
|
||||||
|
assertEquals(3, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingIndexOf_thenReturnFirstNonRepeatingElement() {
|
||||||
|
int result = FirstNonRepeatingElement.findFirstNonRepeatedElementUsingIndex(list);
|
||||||
|
assertEquals(3, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingHashMap_thenReturnFirstNonRepeatingElement() {
|
||||||
|
int result = FirstNonRepeatingElement.findFirstNonRepeatingUsingHashMap(list);
|
||||||
|
assertEquals(3, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenUsingArray_thenReturnFirstNonRepeatingElement() {
|
||||||
|
int result = FirstNonRepeatingElement.findFirstNonRepeatingUsingArray(list);
|
||||||
|
assertEquals(3, result);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user