Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
11e21fc
michaelmoss java-collections
michael77737 Mar 6, 2026
e755c7e
michaelmoss java-collections
michael77737 Mar 6, 2026
c022c66
michaelmoss java-collections
michael77737 Mar 6, 2026
9b71446
michaelmoss java-collections
michael77737 Mar 6, 2026
097198a
michaelmoss java-collections
michael77737 Mar 6, 2026
a94778c
michaelmoss java-collections
michael77737 Mar 7, 2026
b704baf
michaelmoss java-collections
michael77737 Mar 7, 2026
fd353f9
michaelmoss java-collections
michael77737 Mar 7, 2026
1745ae9
michaelmoss java-collections
michael77737 Mar 7, 2026
b9c59b1
michaelmoss java-collections
michael77737 Mar 7, 2026
1c1345c
michaelmoss java-collections
michael77737 Mar 7, 2026
76624b1
michaelmoss java-collections
michael77737 Mar 7, 2026
1c19217
michaelmoss java-collections
michael77737 Mar 7, 2026
89db66a
michaelmoss java-collections
michael77737 Mar 7, 2026
84e2702
michaelmoss java-collections
michael77737 Mar 7, 2026
bc7bbd8
michaelmoss java-collections
michael77737 Mar 7, 2026
6c5806a
michaelmoss java-collections
michael77737 Mar 7, 2026
596f972
michaelmoss java-collections
michael77737 Mar 7, 2026
edbaa23
michaelmoss java-collections
michael77737 Mar 7, 2026
29f76fd
michaelmoss java-collections
michael77737 Mar 7, 2026
70a3166
michaelmoss java-collections
michael77737 Mar 7, 2026
0ea595e
michaelmoss java-collections
michael77737 Mar 7, 2026
d2e3e79
michaelmoss java-collections
michael77737 Mar 7, 2026
ac90e78
michaelmoss java-collections
michael77737 Mar 7, 2026
9edf4fb
michaelmoss java-collections
michael77737 Mar 7, 2026
b1af621
michaelmoss java-collections
michael77737 Mar 7, 2026
3f0e000
michaelmoss java-collections
michael77737 Mar 7, 2026
9e14e61
michaelmoss java-collections
michael77737 Mar 7, 2026
ee64f15
michaelmoss java-collections
michael77737 Mar 7, 2026
a638e7e
michaelmoss java-collections
michael77737 Mar 7, 2026
8019f71
michaelmoss java-collections
michael77737 Mar 7, 2026
610ba66
michaelmoss java-collections
michael77737 Mar 7, 2026
3b1c737
michaelmoss java-collections
michael77737 Mar 7, 2026
4d13f83
michaelmoss java-collections
michael77737 Mar 7, 2026
a5c362e
michaelmoss java-collections
michael77737 Mar 7, 2026
b335401
michaelmoss java-collections
michael77737 Mar 7, 2026
e1df3c9
java-collections
michael77737 Mar 7, 2026
5efc46f
michaelmoss java-collections
michael77737 Mar 7, 2026
3873c06
michaelmoss java-loops
michael77737 Mar 7, 2026
af745ca
michaelmoss java-collections
michael77737 Mar 7, 2026
8cc8a10
michaelmoss java-collections
michael77737 Mar 7, 2026
9e34580
michaelmoss java-collections
michael77737 Mar 7, 2026
d35e51d
michaelmoss java-collections
michael77737 Mar 7, 2026
4379777
michaelmoss java-collections
michael77737 Mar 7, 2026
fba3ef6
michaelmoss java-collections
michael77737 Mar 7, 2026
2ebb2c2
michaelmoss java-collections
michael77737 Mar 7, 2026
6d88db9
michaelmoss java-collections
michael77737 Mar 7, 2026
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
35 changes: 33 additions & 2 deletions src/main/java/Collections/Practice/CollectionBasics.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public static int sum(Collection<Integer> numbers) {
// TODO:
// Loop through the collection
// Add each number to total
for (Integer num : numbers) {
total += num;
}

return total;
}
Expand All @@ -48,7 +51,12 @@ public static int countEven(Collection<Integer> numbers) {

// TODO:
// Loop through the collection
for (Integer num : numbers) {
// If the number is even, increase count
if (num % 2 == 0) {
count++;
}
}

return count;
}
Expand All @@ -64,7 +72,13 @@ public static int findMax(Collection<Integer> numbers) {

// TODO:
// Loop through numbers
for (Integer num : numbers) {
// Update max if current number is larger
if (num > max) {
max = num;
}
}


return max;
}
Expand All @@ -77,10 +91,15 @@ public static int findMax(Collection<Integer> numbers) {
*/
public static boolean hasDuplicates(Collection<Integer> numbers) {




// TODO:
// Hint:
// Compare the size of a collection with the size of a Set



return false;
}

Expand All @@ -96,6 +115,11 @@ public static int countOccurrences(Collection<Integer> numbers, int target) {
// TODO:
// Loop through numbers
// If number equals target, increase count
for (Integer num : numbers) {
if (num == target) {
count++;
}
}

return count;
}
Expand All @@ -112,8 +136,15 @@ public static Collection<Integer> filterGreaterThanTwenty(Collection<Integer> nu

// TODO:
// Loop through numbers
// Add numbers greater than 20 to result
for (Integer num : numbers) {
// Add numbers greater than 20 to result
if (num > 20) {
result.add(num);

}
}


return result;
}
}
}
162 changes: 152 additions & 10 deletions src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package CollectionsHackerrank;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Stack;


public class CollectionsHackerrankProblems {
public class CollectionsHackerrankPractice {
public static class CollectionsHackerrankPractice {

public static void main(String[] args) {

// You can test your methods here
List<Integer> numbers = Arrays.asList(1,2,2,3,4,4,5);
System.out.println(removeDuplicates(numbers));
}



}

Expand All @@ -24,8 +37,14 @@ public static void main(String[] args) {
public static List<Integer> removeDuplicates(List<Integer> numbers) {

// TODO: Implement this method
Set<Integer> uniqueNumbers = new LinkedHashSet<>(numbers); // removes duplicates
List<Integer> result = new ArrayList<>(uniqueNumbers);




return result;

return null;
}

/*
Expand All @@ -39,10 +58,22 @@ public static List<Integer> removeDuplicates(List<Integer> numbers) {
public static Map<Integer, Integer> countFrequency(List<Integer> numbers) {

// TODO: Implement this method
Map<Integer, Integer> frequencyMap = new HashMap<>();

return null;
for (Integer num : numbers) {
if (frequencyMap.containsKey(num)) {
frequencyMap.put(num, frequencyMap.get(num) + 1);
} else {
frequencyMap.put(num, 1);
}
}

return frequencyMap;
}




/*
Problem 3
Return the first number that appears only once.
Expand All @@ -54,6 +85,18 @@ public static Map<Integer, Integer> countFrequency(List<Integer> numbers) {
public static Integer firstUnique(List<Integer> numbers) {

// TODO: Implement this method
Map<Integer, Integer> frequencyMap = new HashMap<>();

for (Integer num : numbers) {
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
}

for (Integer num : numbers) {
if (frequencyMap.get(num) == 1) {
return num;
}
}


return null;
}
Expand All @@ -71,10 +114,23 @@ public static Integer firstUnique(List<Integer> numbers) {
public static boolean twoSum(List<Integer> numbers, int target) {

// TODO: Implement this method
Set<Integer> seen = new HashSet<>();

for (int num : numbers) {
int complement = target - num;

if (seen.contains(complement)) {
return true;
}

seen.add(num);
}

return false;
}



/*
Problem 5
Count how many unique words exist in a list.
Expand All @@ -86,10 +142,16 @@ public static boolean twoSum(List<Integer> numbers, int target) {
public static int countUniqueWords(List<String> words) {

// TODO: Implement this method
Set<String> uniqueWords = new HashSet<>();

for (String word : words) {
uniqueWords.add(word);
}

return 0;
return uniqueWords.size();
}


/*
Problem 6
Reverse a queue.
Expand All @@ -101,10 +163,23 @@ public static int countUniqueWords(List<String> words) {
public static Queue<Integer> reverseQueue(Queue<Integer> queue) {

// TODO: Implement this method
Stack<Integer> stack = new Stack<>();

return null;
// Move elements from queue to stack
while (!queue.isEmpty()) {
stack.push(queue.poll());
}

// Move elements back to queue
while (!stack.isEmpty()) {
queue.add(stack.pop());
}

return queue;
}



/*
Problem 7
Determine whether parentheses are balanced.
Expand All @@ -119,10 +194,28 @@ public static Queue<Integer> reverseQueue(Queue<Integer> queue) {
public static boolean isBalanced(String expression) {

// TODO: Implement this method
Stack<Character> stack = new Stack<>();

return false;
for (char c : expression.toCharArray()) {

if (c == '(') {
stack.push(c);
}
else if (c == ')') {

if (stack.isEmpty()) {
return false;
}

stack.pop();
}
}

return stack.isEmpty();
}



/*
Problem 8
Return the number that appears most frequently in the list.
Expand All @@ -134,10 +227,27 @@ public static boolean isBalanced(String expression) {
public static Integer mostFrequent(List<Integer> numbers) {

// TODO: Implement this method
Map<Integer, Integer> frequency = new HashMap<>();

return null;
for (Integer num : numbers) {
frequency.put(num, frequency.getOrDefault(num, 0) + 1);
}

int maxCount = 0;
Integer result = null;

for (Map.Entry<Integer, Integer> entry : frequency.entrySet()) {
if (entry.getValue() > maxCount) {
maxCount = entry.getValue();
result = entry.getKey();
}
}

return result;
}



/*
Problem 9
Group words based on their length.
Expand All @@ -154,10 +264,24 @@ public static Integer mostFrequent(List<Integer> numbers) {
public static Map<Integer, List<String>> groupByLength(List<String> words) {

// TODO: Implement this method
Map<Integer, List<String>> map = new HashMap<>();

return null;
for (String word : words) {

int length = word.length();

if (!map.containsKey(length)) {
map.put(length, new ArrayList<>());
}

map.get(length).add(word);
}

return map;
}



/*
Problem 10
Return the maximum sum of any window of size k.
Expand All @@ -171,8 +295,26 @@ public static Map<Integer, List<String>> groupByLength(List<String> words) {
public static int maxSlidingWindowSum(List<Integer> numbers, int k) {

// TODO: Implement this method
int windowSum = 0;

return 0;
// First window
for (int i = 0; i < k; i++) {
windowSum += numbers.get(i);
}

int maxSum = windowSum;

// Slide the window
for (int i = k; i < numbers.size(); i++) {
windowSum += numbers.get(i); // add next
windowSum -= numbers.get(i - k); // remove previous
maxSum = Math.max(maxSum, windowSum);
}

return maxSum;
}



}
}

13 changes: 13 additions & 0 deletions src/main/java/Iterable/Examples/ForEachLoopDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ public static void main(String[] args) {

// TODO:
// Use a for-each loop to print each student name
for (String student : students) {
System.out.println(student);
}



System.out.println("\nPrinting students in uppercase:");

// TODO:
// Use a for-each loop to print each name in uppercase
for (String student : students) {
System.out.println(student.toUpperCase());
}



System.out.println("\nCount the number of students:");
Expand All @@ -31,6 +39,11 @@ public static void main(String[] args) {

// TODO:
// Use a for-each loop to count how many students are in the list
for (String student : students) {
System.out.println(student.toUpperCase());
}



System.out.println("Total students: " + count);
}
Expand Down
Loading