From 35ef9393c1ffe17562cbd6812c3a9dd196414c36 Mon Sep 17 00:00:00 2001 From: Wynn Teo <49014791+wynnteo@users.noreply.github.com> Date: Sat, 10 Feb 2024 10:26:25 +0800 Subject: [PATCH] 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 --- .../FirstNonRepeatingElement.java | 61 +++++++++++++++++++ .../FirstNonRepeatingElementUnitTest.java | 43 +++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 algorithms-modules/algorithms-searching/src/main/java/com/baeldung/algorithms/firstnonrepeating/FirstNonRepeatingElement.java create mode 100644 algorithms-modules/algorithms-searching/src/test/java/com/baeldung/algorithms/firstnonrepeating/FirstNonRepeatingElementUnitTest.java diff --git a/algorithms-modules/algorithms-searching/src/main/java/com/baeldung/algorithms/firstnonrepeating/FirstNonRepeatingElement.java b/algorithms-modules/algorithms-searching/src/main/java/com/baeldung/algorithms/firstnonrepeating/FirstNonRepeatingElement.java new file mode 100644 index 0000000000..0791066143 --- /dev/null +++ b/algorithms-modules/algorithms-searching/src/main/java/com/baeldung/algorithms/firstnonrepeating/FirstNonRepeatingElement.java @@ -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 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 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 list) { + Map 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 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; + } +} diff --git a/algorithms-modules/algorithms-searching/src/test/java/com/baeldung/algorithms/firstnonrepeating/FirstNonRepeatingElementUnitTest.java b/algorithms-modules/algorithms-searching/src/test/java/com/baeldung/algorithms/firstnonrepeating/FirstNonRepeatingElementUnitTest.java new file mode 100644 index 0000000000..76c300303a --- /dev/null +++ b/algorithms-modules/algorithms-searching/src/test/java/com/baeldung/algorithms/firstnonrepeating/FirstNonRepeatingElementUnitTest.java @@ -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 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); + } +}