diff --git a/src/Practice.java b/src/Practice.java index ab2d1c5..d709161 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -5,8 +5,8 @@ public class Practice { - // Time Complexity: - // Space Complexity: + // Time Complexity: O(n) + // Space Complexity: O(n) public static List findEvens(int[] array) { List evens = new ArrayList<>(); for (int num : array) { @@ -17,8 +17,8 @@ public static List findEvens(int[] array) { return evens; } - // Time Complexity: - // Space Complexity: + // Time Complexity: O(n) + // Space Complexity: O(1) public static int sumDiagonal(int[][] matrix) { int sum = 0; for (int i = 0; i < matrix.length; i++) { @@ -26,10 +26,9 @@ public static int sumDiagonal(int[][] matrix) { } return sum; } - - // Time Complexity: - // Space Complexity: + // Time Complexity: O(n) + // Space Complexity: O(n^2) // Does the 'T' look confusing? Consider refreshing on generic methods // We'll revisit generics as a class later public static Map countFrequencies(T[] array) { @@ -49,8 +48,8 @@ public static Map countFrequencies(T[] array) { * * Once you finish, WRITE TESTS FOR IT in PracticeTest.java * - * Time Complexity: - * Space Complexity: + * Time Complexity: O(n) + * Space Complexity: O(n) * * @param nums An array of integers * @return the integer that shows up most commonly @@ -58,7 +57,25 @@ public static Map countFrequencies(T[] array) { public static int mostCommonTimeEfficient(int[] nums) { // TODO: Complete this method with an implementation that runs // in O(n) time. n = nums.size() - return -1; + + HashMap frequencies = new HashMap<>(); + int highestCount = 0; + int mostCommon = 0; + for (int i = 0; i < nums.length; i++) { + if (frequencies.containsKey(nums[i])) { + int count = frequencies.get(nums[i]); + count++; + frequencies.put(nums[i], count); + } else { + frequencies.put(nums[i], 1); + } + if (frequencies.get(nums[i]) >= highestCount) { + highestCount = frequencies.get(nums[i]); + mostCommon = nums[i]; + } + } + + return mostCommon; } /** @@ -70,8 +87,8 @@ public static int mostCommonTimeEfficient(int[] nums) { * * Once you finish, WRITE TESTS FOR IT in PracticeTest.java * - * Time Complexity: - * Space Complexity: + * Time Complexity: + * Space Complexity: * * @param nums An array of integers * @return the integer that shows up most commonly @@ -79,6 +96,28 @@ public static int mostCommonTimeEfficient(int[] nums) { public static int mostCommonSpaceEfficient(int[] nums) { // TODO: Complete this method with an implementation that runs // in O(1) space. - return -1; + + // HashMap that stores the number without duplicates + Map mostCommonMap = new HashMap<>(); + // Keeps track of the most common number + int mostCommonCount = 0; + int mostCommonInt = nums[0]; + + // Loops over the input array to add each number to the HashMap + // Sets each numbers count to 1 + for (int num : nums) { + mostCommonMap.put(num, mostCommonMap.getOrDefault(num, 0)); + + // Variable that targets the count, that being the number that represents the + // amount of times a number appears in the array + int count = mostCommonMap.get(num); + + if (count > mostCommonCount) { + mostCommonCount = count; + mostCommonInt = num; + } + } + + return mostCommonInt; } } \ No newline at end of file diff --git a/src/PracticeTest.java b/src/PracticeTest.java index a0bdf6c..5b6d95e 100644 --- a/src/PracticeTest.java +++ b/src/PracticeTest.java @@ -1,11 +1,25 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; - public class PracticeTest { - - // TODO: Implement tests for Practice.mostCommonTimeEfficient and Practice.mostCommonSpaceEfficient - // Hints: They are static methods, so you will use the full Practice.mostCommonTimeEfficient for method calls - // Forgot how to do unit tests? Look back at the ramblebot tests or old slides for a refresher! + // TODO: Implement tests for Practice.mostCommonTimeEfficient and + // Practice.mostCommonSpaceEfficient + + // Hints: They are static methods, so you will use the full + // Practice.mostCommonTimeEfficient for method calls + // Forgot how to do unit tests? Look back at the ramblebot tests or old slides + // for a refresher! + + @Test + void mostCommonTimeEfficient() { + int[] nums = { 0, 1, 1, 2, 3, 4, 5, 6, 7, 8 }; + assertEquals(1, Practice.mostCommonTimeEfficient(nums)); + } + + @Test + void mostCommonSpaceEfficient() { + int[] nums = { 2, 2, 2, 2, 4, 4, 6, 6, 6, 8, 9, 10 }; + assertEquals(2, Practice.mostCommonSpaceEfficient(nums)); + } }