diff --git a/src/Practice.java b/src/Practice.java index 6795da7..a0fb8a0 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -5,8 +5,9 @@ public class Practice { - // Time Complexity: - // Space Complexity: + // Time Complexity: 0(n) + // Space Complexity: 0(n) + public static List findEvens(int[] array) { List evens = new ArrayList<>(); for (int num : array) { @@ -17,8 +18,8 @@ public static List findEvens(int[] array) { return evens; } - // Time Complexity: - // Space Complexity: + // Time Complexity: 0(n) + // Space Complexity: 0(1) public static int sumDiagonal(int[][] matrix) { int sum = 0; for (int i = 0; i < matrix.length; i++) { @@ -28,8 +29,8 @@ public static int sumDiagonal(int[][] matrix) { } - // Time Complexity: - // Space Complexity: + // Time Complexity: 0(n) + // Space Complexity: 0(n) // Does the 'T' look confusing? Consider refreshing on generic methods // We'll revisit generics as a class later public static Map countFrequencies(T[] array) { @@ -40,8 +41,8 @@ public static Map countFrequencies(T[] array) { return frequencies; } - // Time Complexity: - // Space Complexity: + // Time Complexity: 0(n2) + // Space Complexity: 0(n) public static List evensToSquare(int n) { List evens = new ArrayList<>(); for(int i = 0; i <= n*n; i+=2) { @@ -68,8 +69,25 @@ public static List evensToSquare(int n) { public static int mostCommonTimeEfficient(int[] nums) { // TODO: Complete this method with an implementation that runs // in O(n) time. n = nums.size() - return -1; + if (nums == null || nums.length == 0) { + throw new IllegalArgumentException("Invalid input"); + } + + Map counts = new HashMap<>(); + int result = nums[0]; + int max = 1; + + for (int num : nums) { + int count = counts.getOrDefault(num, 0) + 1; + counts.put(num, count); + if (count > max) { + max = count; + result = num; + } } + + return result; +} /** * Returns the integer that shows up most frequently in an array. @@ -80,15 +98,36 @@ public static int mostCommonTimeEfficient(int[] nums) { * * Once you finish, WRITE TESTS FOR IT in PracticeTest.java * - * Time Complexity: - * Space Complexity: + * Time Complexity: 0(n2) + * Space Complexity: 0(1) * * @param nums An array of integers * @return the integer that shows up most commonly */ - public static int mostCommonSpaceEfficient(int[] nums) { - // TODO: Complete this method with an implementation that runs - // in O(1) space. - return -1; - } + public static int mostCommonSpaceEfficient(int[] nums) { + if (nums == null || nums.length == 0) { + throw new IllegalArgumentException("Input array is empty or null"); + } + + int maxCount = 0; + int mostCommon = nums[0]; + + for (int i = 0; i < nums.length; i++) { + int currentNum = nums[i]; + int currentCount = 0; + + for (int j = 0; j < nums.length; j++) { + if (nums[j] == currentNum) { + currentCount++; + } + } + + if (currentCount > maxCount) { + maxCount = currentCount; + mostCommon = currentNum; + } + } + + return mostCommon; +} } \ No newline at end of file diff --git a/src/PracticeTest.java b/src/PracticeTest.java index 4242545..8bc3078 100644 --- a/src/PracticeTest.java +++ b/src/PracticeTest.java @@ -1,10 +1,43 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +import java.util.HashMap; +import java.util.Map; + public class PracticeTest { // TODO: Implement tests for Practice.mostCommonTimeEfficient and Practice.mostCommonSpaceEfficient + // Tests for mostCommonTimeEfficient + public static void main(String[] args) { + testMostCommon(); + } + + public static void testMostCommon() { + + int result1 = Practice.mostCommonTimeEfficient(new int[]{1,2,3,3}); + System.out.println("Test 1: " + (result1 == 3 ? "PASSED" : "FAILED")); + + + int result2 = Practice.mostCommonTimeEfficient(new int[]{2,2,1,1,2}); + System.out.println("Test 2: " + (result2 == 2 ? "PASSED" : "FAILED")); + + + int result3 = Practice.mostCommonTimeEfficient(new int[]{1,2,3}); + System.out.println("Test 3: " + (result3 == 1 ? "PASSED" : "FAILED")); + } + + public static void testMostCommonSpaceEfficient() { + + int result1 = Practice.mostCommonSpaceEfficient(new int[]{5}); + System.out.println("Test 1 (Single element): " + (result1 == 5 ? "PASSED" : "FAILED")); - // Hints: They are static methods, so you will use the full Practice.mostCommonTimeEfficient for method calls + + int result2 = Practice.mostCommonSpaceEfficient(new int[]{1, 2, 2, 3, 3, 3}); + System.out.println("Test 2 (Clear majority): " + (result2 == 3 ? "PASSED" : "FAILED")); + + + int result3 = Practice.mostCommonSpaceEfficient(new int[]{1, 2, 2, 1}); + System.out.println("Test 3 (Tie breaker): " + (result3 == 1 ? "PASSED" : "FAILED")); } +} \ No newline at end of file diff --git a/src/tests.java b/src/tests.java new file mode 100644 index 0000000..f11fb18 --- /dev/null +++ b/src/tests.java @@ -0,0 +1,52 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +public class tests { + + @Test + public void testMostCommonTimeEfficient_SingleElement() { + int[] nums = {5}; + assertEquals(5, Practice.mostCommonTimeEfficient(nums)); + } + + @Test + public void testMostCommonTimeEfficient_AllSameElements() { + int[] nums = {2, 2, 2, 2}; + assertEquals(2, Practice.mostCommonTimeEfficient(nums)); + } + + @Test + public void testMostCommonTimeEfficient_OneMostCommon() { + int[] nums = {1, 2, 2, 3, 3, 3, 4}; + assertEquals(3, Practice.mostCommonTimeEfficient(nums)); + } + + @Test + public void testMostCommonTimeEfficient_TieBreakerFirstWins() { + int[] nums = {1, 2, 2, 1, 1, 2, 3}; + assertEquals(1, Practice.mostCommonTimeEfficient(nums)); + } + + @Test + public void mostCommonSpaceEfficient_mixedNums() { + int[] arr = {1, 2, 3, 4, 3, 4, 4}; + int actual = Practice.mostCommonSpaceEfficient(arr); + assertEquals(4, actual); + } + + @Test + public void mostCommonSpaceEfficient_tie() { + int[] arr = {1, 2, 2, 3, 4, 4}; + int actual = Practice.mostCommonSpaceEfficient(arr); + assertEquals(2, actual); + } + + @Test + public void mostCommonSpaceEfficient_noDups() { + int[] arr = {1, 2, 3, 4}; + int actual = Practice.mostCommonSpaceEfficient(arr); + assertEquals(1, actual); + } + + // Hints: They are static methods, so you will use the full Practice.mostCommonTimeEfficient for method calls +} \ No newline at end of file