diff --git a/src/Practice.java b/src/Practice.java index 6795da7..40dfc9f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,12 +1,12 @@ import java.util.ArrayList; -import java.util.List; import java.util.HashMap; +import java.util.List; import java.util.Map; public class Practice { - // Time Complexity: - // Space Complexity: + // Time Complexity: o(n) n is array size + // Space Complexity: o(n) n is new list 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) n is matrix length + // Space Complexity: o(1) public static int sumDiagonal(int[][] matrix) { int sum = 0; for (int i = 0; i < matrix.length; i++) { @@ -27,9 +27,9 @@ public static int sumDiagonal(int[][] matrix) { return sum; } - - // Time Complexity: - // Space Complexity: +//n = T + // 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) { @@ -40,8 +40,9 @@ public static Map countFrequencies(T[] array) { return frequencies; } - // Time Complexity: - // Space Complexity: +//n = n*n + // Time Complexity: O(n) + // Space Complexity: O(n) public static List evensToSquare(int n) { List evens = new ArrayList<>(); for(int i = 0; i <= n*n; i+=2) { @@ -59,8 +60,8 @@ public static List evensToSquare(int n) { * * Once you finish, WRITE TESTS FOR IT in PracticeTest.java * - * Time Complexity: - * Space Complexity: + * Time Complexity: O(n) + * Space Complexity: O(n) (potentially n number of entries) * * @param nums An array of integers * @return the integer that shows up most commonly @@ -68,8 +69,23 @@ 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; - } + + Map mapOfCommonNums = new HashMap<>(); + int maxCount = 0; + int mostCommon = nums[0]; + + // Loop through the array and count how many times a number is read through + for (int commonNumber : nums) { + mapOfCommonNums.put(commonNumber, mapOfCommonNums.getOrDefault(commonNumber,0)+ 1 ); + // checks if the common number is greater than the maxCount and then sets it to mostCommon + if (mapOfCommonNums.get(commonNumber) > maxCount) { + maxCount = mapOfCommonNums.get(commonNumber); + mostCommon = commonNumber; + }//end if + }//end for + + return mostCommon; + }//end mostCommonTimeEfficient /** * Returns the integer that shows up most frequently in an array. @@ -80,8 +96,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) + * Space Complexity: o(1) * * @param nums An array of integers * @return the integer that shows up most commonly @@ -89,6 +105,25 @@ 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; + + //empty array + if(nums.length == 0) + return -1; + + int count = 0; + int possibleNum = 0; + + //finding the num that shows up most (tiebreaker built in) + for(int i = nums.length-1; i >= 0; i--) + { + if(count == 0) //starting number and finding recurring num + possibleNum = nums[i]; + if(nums[i] == possibleNum) //checking if num[i] value has already appeared, ++ + count ++; + if(nums[i] != possibleNum) //if it hasn't appeared then -- + count--; + } + + return possibleNum; } } \ No newline at end of file diff --git a/src/PracticeTest.java b/src/PracticeTest.java index 4242545..ced6234 100644 --- a/src/PracticeTest.java +++ b/src/PracticeTest.java @@ -7,4 +7,88 @@ 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 -} + + //tests for mostCommonTimeEfficient + + @Test + public void timeEfficientMostCommonNumWorking() { + // Arrange + int[] numbers = {1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7}; + + // Act + int result = Practice.mostCommonTimeEfficient(numbers); + + // Assert + assertEquals(7, result); //expected res: 7, most common number + }//end working + + @Test + public void timeEfficientMostCommonNumTie() { + // Arrange + int[] nums = {1, 1, 2, 2, 3, 3}; + + // Act + int result = Practice.mostCommonTimeEfficient(nums); + + // Assert + assertEquals(1, result); //expected res: 1, the first most common number is never overwritten since its a tie + }//end tie + + @Test + public void timeEfficientMostCommonNumOutOfOrder() { + // Arrange + int[] numerals = {1, 2, 3, 2, 3, 2, 3, 7, 3}; + + // Act + int result = Practice.mostCommonTimeEfficient(numerals); + + // Assert + assertEquals(3, result); //expected res: 3, the most common number but out of order + }// end outOfOrder + + + //tests for mostCommonSpaceEfficient + + @Test + public void spaceEfficientMostCommonNumWorking() { + // Arrange + int[] numbers = {1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7}; + + // Act + int result = Practice.mostCommonSpaceEfficient(numbers); + + // Assert + assertEquals(7, result); //expected res: 7, most common number + }//end working + + @Test + public void spaceEfficientMostCommonNumTie() { + // Arrange + int[] nums = {1, 1, 2, 2, 3, 3}; + + // Act + int result = Practice.mostCommonSpaceEfficient(nums); + + // Assert + assertEquals(1, result); //expected res: 1, the first most common number is never overwritten since its a tie + }//end tie + + @Test + public void spaceEfficientMostCommonNumOutOfOrder() { + // Arrange + int[] numerals = {1, 2, 3, 2, 3, 2, 3, 7, 3}; + + // Act + int result = Practice.mostCommonSpaceEfficient(numerals); + + // Assert + assertEquals(3, result); //expected res: 3, the most common number but out of order + }// end outOfOrder + + + + + + + +}//end practiceTest