diff --git a/src/Practice.java b/src/Practice.java index ab2d1c5..bb786fb 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++) { @@ -28,8 +28,8 @@ public static int sumDiagonal(int[][] matrix) { } - // Time Complexity: - // Space Complexity: + // Time Complexity: O(n) + // Space Complexity: O(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) { @@ -49,8 +49,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 +58,29 @@ 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; + if (nums.length == 0 || nums == null) { + throw new IllegalArgumentException("Array is null or empty"); + } else { + // will hold number at first index and initiate count + int number = nums[0]; + int freqCount = 0; + // stores numbers and frequencies in a hashmap + Map numCount = new HashMap<>(); + // iterate through and count, update number/count if one with more apperances is found + for (int num: nums) { + int count = numCount.getOrDefault(num, 0) + 1; + numCount.put(num, count); + + if (count > freqCount) { + number = num; + freqCount = count; + } else if (count == freqCount && num > number) { + number = num; + } + } + // return most frequent number + return number; + } } /** @@ -70,8 +92,8 @@ public static int mostCommonTimeEfficient(int[] nums) { * * Once you finish, WRITE TESTS FOR IT in PracticeTest.java * - * Time Complexity: - * Space Complexity: + * Time Complexity: O(n^2) + * Space Complexity: O(1) * * @param nums An array of integers * @return the integer that shows up most commonly @@ -79,6 +101,29 @@ 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; + if (nums.length == 0 || nums == null) { + throw new IllegalArgumentException("Array is null or empty"); + } else if (nums.length == 1) { + return nums[0]; + } else { + int number = 0; + int count = 0; + for (int i : nums) { + int currentNum = i; + int currentCount = 0; + for (int j = 0; j < nums.length; j++) { + if (currentNum == nums[j]) { + currentCount++; + } + } + if (currentCount > count) { + count = currentCount; + number = currentNum; + } else if (currentCount == count && currentNum > number) { + number = currentNum; + } + } + return number; + } } } \ No newline at end of file diff --git a/src/PracticeTest.java b/src/PracticeTest.java index a0bdf6c..25a53e4 100644 --- a/src/PracticeTest.java +++ b/src/PracticeTest.java @@ -8,4 +8,78 @@ public class PracticeTest { // 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 testMostCommonTimeEfficient() { + int[] nums = {4, 4, 2, 5, 1, 7, 3}; + int commonNum = Practice.mostCommonTimeEfficient(nums); + assertEquals(4, commonNum); + } + + @Test + void testMostCommonSpaceEfficient() { + int[] nums = {3, 3, 4, 5, 1, 3, 3}; + int commonNum = Practice.mostCommonSpaceEfficient(nums); + assertEquals(3, commonNum); + } + + @Test + void testMostCommonSpaceEfficientOneElement() { + int[] nums = {3}; + int commonNum = Practice.mostCommonSpaceEfficient(nums); + assertEquals(3, commonNum); + } + + @Test + void testMostCommonTimeEfficientOneElement() { + int[] nums = {3}; + int commonNum = Practice.mostCommonTimeEfficient(nums); + assertEquals(3, commonNum); + } + + @Test + void testMostCommonSpaceEfficientTie() { + int[] nums = {7, 7, 2, 2, 1, 4, 4, 1}; + int commonNum = Practice.mostCommonSpaceEfficient(nums); + assertEquals(7, commonNum); + } + + @Test + void testMostCommonTimeEfficientTie() { + int[] nums = {7, 7, 9, 9, 1, 8, 8, 1}; + int commonNum = Practice.mostCommonTimeEfficient(nums); + assertEquals(9, commonNum); + } + + @Test + void testMostCommonTimeEfficientSameElements() { + int[] nums = {9, 9, 9, 9}; + int commonNum = Practice.mostCommonTimeEfficient(nums); + assertEquals(9, commonNum); + } + + @Test + void testMostCommonSpaceEfficientSameElements() { + int[] nums = {9, 9, 9, 9}; + int commonNum = Practice.mostCommonSpaceEfficient(nums); + assertEquals(9, commonNum); + } + + @Test + void testMostCommonTimeEfficientNoElements() { + int[] nums = {}; + // Uses a lambda expression to wrap the method call to be tested + assertThrows(IllegalArgumentException.class, () -> { + Practice.mostCommonTimeEfficient(nums); + }); + } + + @Test + void testMostCommonSpaceEfficientNoElements() { + int[] nums = {}; + // Uses a lambda expression to wrap the method call to be tested + assertThrows(IllegalArgumentException.class, () -> { + Practice.mostCommonSpaceEfficient(nums); + }); + } }