diff --git a/src/Practice.java b/src/Practice.java index 6795da7..49451f4 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,12 +1,14 @@ import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; public class Practice { - // Time Complexity: - // Space Complexity: + // Time Complexity: O(n) + // Space Complexity: O(n) + // n is the length of array public static List findEvens(int[] array) { List evens = new ArrayList<>(); for (int num : array) { @@ -17,8 +19,9 @@ public static List findEvens(int[] array) { return evens; } - // Time Complexity: - // Space Complexity: + // Time Complexity: O(n) + // Space Complexity: O(1) + // n is the length of the matrix public static int sumDiagonal(int[][] matrix) { int sum = 0; for (int i = 0; i < matrix.length; i++) { @@ -28,8 +31,9 @@ public static int sumDiagonal(int[][] matrix) { } - // Time Complexity: - // Space Complexity: + // Time Complexity: O(n) + // Space Complexity: O(n)) + // n is the length of array // 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 +44,9 @@ public static Map countFrequencies(T[] array) { return frequencies; } - // Time Complexity: - // Space Complexity: + // Time Complexity: O(n^2) + // Space Complexity: O(n^2) + // n is the value of int n public static List evensToSquare(int n) { List evens = new ArrayList<>(); for(int i = 0; i <= n*n; i+=2) { @@ -59,16 +64,49 @@ 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) + * // n is the length of nums * * @param nums An array of integers * @return the integer that shows up most commonly */ - public static int mostCommonTimeEfficient(int[] nums) { + 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) + { + return -1; + } + + Map intMap = new HashMap<>(); + for(Integer num : nums) + { + if(intMap.containsKey(num)) + { + intMap.put(num, intMap.get(num) + 1 ); + } + else{ + intMap.put(num, 1 ); + } + } + + int maxValue = 0; + int keyWithMaxValue = 0; + + + for (int key : intMap.keySet()) + { + if (intMap.get(key) > maxValue) + { + maxValue = intMap.get(key); + keyWithMaxValue = key; + } + } + return keyWithMaxValue; + } /** @@ -86,9 +124,43 @@ public static int mostCommonTimeEfficient(int[] nums) { * @param nums An array of integers * @return the integer that shows up most commonly */ - public static int mostCommonSpaceEfficient(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) + { + return -1; + } + + Arrays.sort(nums); //sorted the list from smallest to largest + + int count = 1; + int maxCount = 1; + int current = nums[0]; + int mostCommon = nums[0]; + + for(int i = 1; i < nums.length; i++) + { + if(nums[i] == current) + { + count ++; + } + + else + { + current = nums[i]; + count = 1; + } + + if (count > maxCount) + { + maxCount = count; + mostCommon = current; + + } + } + return mostCommon; } } \ No newline at end of file diff --git a/src/PracticeTest.java b/src/PracticeTest.java index 4242545..b78fd0d 100644 --- a/src/PracticeTest.java +++ b/src/PracticeTest.java @@ -7,4 +7,124 @@ 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 method // + +@Test +void mostCommonTimeEfficient_SingleElement() +{ + // arrange + int[] testArray = {100}; + // act + int actual = Practice.mostCommonTimeEfficient(testArray); + //assert + assertEquals(100, actual); +} + +@Test +void mostCommonTimeEfficient_OneMostCommonValue() +{ + // arrange + int[] testArray = {1, 2, 3, 1}; + // act + int actual = Practice.mostCommonTimeEfficient(testArray); + //assert + assertEquals(1, actual); +} + +@Test +void mostCommonTimeEfficient_MultipleMostCommonValues() +{ + // arrange + int[] testArray = {1, 2, 3, 1, 2, 2, 3, 3, 1}; + // act + int actual = Practice.mostCommonTimeEfficient(testArray); + //assert + assertEquals(1, actual); +} + +@Test +void mostCommonTimeEfficient_EmptyArray() +{ + // arrange + int[] testArray = {}; + // act + int actual = Practice.mostCommonTimeEfficient(testArray); + //assert + assertEquals(-1, actual); +} + +@Test +void mostCommonTimeEfficient_NegativeElements() +{ + // arrange + int[] testArray = {-1, 1, -2, 3, -1, 2, -3}; + // act + int actual = Practice.mostCommonTimeEfficient(testArray); + //assert + assertEquals(-1, actual); +} + + + + + +//Tests for most common space efficient +@Test +void mostCommonSpaceEfficient_SingleElement() +{ + // arrange + int[] testArray = {100}; + // act + int actual = Practice.mostCommonSpaceEfficient(testArray); + //assert + assertEquals(100, actual); +} + +@Test +void mostCommonSpaceEfficient_OneMostCommonValue() +{ + // arrange + int[] testArray = {1, 2, 3, 1}; + // act + int actual = Practice.mostCommonSpaceEfficient(testArray); + //assert + assertEquals(1, actual); +} + +@Test +void mostCommonSpaceEfficient_MultipleMostCommonValues() +{ + // arrange + int[] testArray = {1, 2, 3, 1, 2, 2, 3, 3, 1}; + // act + int actual = Practice.mostCommonSpaceEfficient(testArray); + //assert + assertEquals(1, actual); +} + +@Test +void mostCommonSpaceEfficient_EmptyArray() +{ + // arrange + int[] testArray = {}; + // act + int actual = Practice.mostCommonSpaceEfficient(testArray); + //assert + assertEquals(-1, actual); +} + +@Test +void mostCommonSpaceEfficient_NegativeElements() +{ + // arrange + int[] testArray = {-1, 1, -2, 3, -1, 2, -3}; + // act + int actual = Practice.mostCommonSpaceEfficient(testArray); + //assert + assertEquals(-1, actual); +} + + + }