Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 55 additions & 16 deletions src/Practice.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

public class Practice {

// Time Complexity:
// Space Complexity:
// Time Complexity: 0(n)
// Space Complexity: 0(n)

public static List<Integer> findEvens(int[] array) {
List<Integer> evens = new ArrayList<>();
for (int num : array) {
Expand All @@ -17,8 +18,8 @@ public static List<Integer> 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++) {
Expand All @@ -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 <T> Map<T, Integer> countFrequencies(T[] array) {
Expand All @@ -40,8 +41,8 @@ public static <T> Map<T, Integer> countFrequencies(T[] array) {
return frequencies;
}

// Time Complexity:
// Space Complexity:
// Time Complexity: 0(n2)
// Space Complexity: 0(n)
public static List<Integer> evensToSquare(int n) {
List<Integer> evens = new ArrayList<>();
for(int i = 0; i <= n*n; i+=2) {
Expand All @@ -68,8 +69,25 @@ public static List<Integer> 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<Integer, Integer> 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.
Expand All @@ -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;
}
}
35 changes: 34 additions & 1 deletion src/PracticeTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
52 changes: 52 additions & 0 deletions src/tests.java
Original file line number Diff line number Diff line change
@@ -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
}