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
36 changes: 28 additions & 8 deletions src/main/java/Collections/Practice/CollectionBasics.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package Collections.Practice;

import java.util.ArrayList;
import java.util.Collection;
import java.util.*;

public class CollectionBasics {
public static void main(String[] args) {
Expand All @@ -10,6 +9,7 @@ public static void main(String[] args) {

numbers.add(4);
numbers.add(8);
//numbers.add(8);
numbers.add(15);
numbers.add(16);
numbers.add(23);
Expand All @@ -33,7 +33,9 @@ public static int sum(Collection<Integer> numbers) {
// TODO:
// Loop through the collection
// Add each number to total

for(Integer sum: numbers){
total += sum;
}
return total;
}

Expand All @@ -49,7 +51,11 @@ public static int countEven(Collection<Integer> numbers) {
// TODO:
// Loop through the collection
// If the number is even, increase count

for(Integer even: numbers){
if(even % 2 == 0){
count++;
}
}
return count;
}

Expand All @@ -65,7 +71,11 @@ public static int findMax(Collection<Integer> numbers) {
// TODO:
// Loop through numbers
// Update max if current number is larger

for(Integer newMax: numbers){
if(newMax > max){
max = newMax;
}
}
return max;
}

Expand All @@ -80,8 +90,9 @@ public static boolean hasDuplicates(Collection<Integer> numbers) {
// TODO:
// Hint:
// Compare the size of a collection with the size of a Set
Set<Integer> diffNumbers = new HashSet<>(numbers);

return false;
return diffNumbers.size() !=numbers.size();
}


Expand All @@ -96,7 +107,11 @@ public static int countOccurrences(Collection<Integer> numbers, int target) {
// TODO:
// Loop through numbers
// If number equals target, increase count

for(Integer nextNum: numbers){
if(nextNum == target){
count++;
}
}
return count;
}

Expand All @@ -113,7 +128,12 @@ public static Collection<Integer> filterGreaterThanTwenty(Collection<Integer> nu
// TODO:
// Loop through numbers
// Add numbers greater than 20 to result

for(Integer overTwenty: numbers){
if(overTwenty > 20){
result.add(overTwenty);
}
}
return result;

}
}
114 changes: 95 additions & 19 deletions src/main/java/CollectionsHackerrank/CollectionsHackerrankProblems.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package CollectionsHackerrank;

import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Queue;
Expand All @@ -22,10 +22,9 @@ public static void main(String[] args) {
Output: [1,2,3,4,5]
*/
public static List<Integer> removeDuplicates(List<Integer> numbers) {

// TODO: Implement this method

return null;
return new ArrayList<>(new LinkedHashSet<>(numbers));
}

/*
Expand All @@ -37,10 +36,14 @@ public static List<Integer> removeDuplicates(List<Integer> numbers) {
Output: {1=1, 2=2, 3=3}
*/
public static Map<Integer, Integer> countFrequency(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);
}

return frequency;
}

/*
Expand All @@ -52,8 +55,14 @@ public static Map<Integer, Integer> countFrequency(List<Integer> numbers) {
Output: 5
*/
public static Integer firstUnique(List<Integer> numbers) {

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

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

return null;
}
Expand All @@ -69,8 +78,19 @@ public static Integer firstUnique(List<Integer> numbers) {
Output: true
*/
public static boolean twoSum(List<Integer> numbers, int target) {

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

for (Integer num : numbers) {

int complement = target - num;

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

seen.add(num);
}

return false;
}
Expand All @@ -86,8 +106,7 @@ public static boolean twoSum(List<Integer> numbers, int target) {
public static int countUniqueWords(List<String> words) {

// TODO: Implement this method

return 0;
return new HashSet<>(words).size();
}

/*
Expand All @@ -99,10 +118,18 @@ public static int countUniqueWords(List<String> words) {
Output: [4,3,2,1]
*/
public static Queue<Integer> reverseQueue(Queue<Integer> queue) {

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

return null;
while (!queue.isEmpty()) {
stack.push(queue.poll());
}

while (!stack.isEmpty()) {
queue.add(stack.pop());
}

return queue;
}

/*
Expand All @@ -117,10 +144,26 @@ public static Queue<Integer> reverseQueue(Queue<Integer> queue) {
Output: false
*/
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);
}

if (c == ')') {

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

stack.pop();
}
}

return stack.isEmpty();
}

/*
Expand All @@ -132,10 +175,21 @@ public static boolean isBalanced(String expression) {
Output: 3
*/
public static Integer mostFrequent(List<Integer> numbers) {

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

return null;
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;
}

/*
Expand All @@ -152,10 +206,19 @@ 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();

map.putIfAbsent(length, new ArrayList<>());

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

return map;
}

/*
Expand All @@ -169,10 +232,23 @@ public static Map<Integer, List<String>> groupByLength(List<String> words) {
Output: 9
*/
public static int maxSlidingWindowSum(List<Integer> numbers, int k) {

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

for (int i = 0; i < numbers.size(); i++) {

windowSum += numbers.get(i);

if (i >= k - 1) {

maxSum = Math.max(maxSum, windowSum);

windowSum -= numbers.get(i - k + 1);
}
}

return 0;
return maxSum;
}
}
}
20 changes: 12 additions & 8 deletions src/main/java/Iterable/Examples/ForEachLoopDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ public static void main(String[] args) {
// TODO:
// Use a for-each loop to print each student name

for(String student: students) {

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

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


System.out.println("\nCount the number of students:");

int count = 0;
for(String student: students) {
System.out.println(student.toUpperCase());
}
System.out.println();

// TODO:
// Use a for-each loop to count how many students are in the list

int count = 0;
for(String student: students) {
count++;
}
System.out.println("Total students: " + count);
}
}
14 changes: 11 additions & 3 deletions src/main/java/Iterable/Examples/IteratorDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void main(String[] args) {

System.out.println("Original list:");
System.out.println(numbers);

//Can you store multiple array lists in an iterator?
// Create iterator
Iterator<Integer> iterator = numbers.iterator();

Expand All @@ -26,7 +26,10 @@ public static void main(String[] args) {
// TODO:
// Use iterator.hasNext() and iterator.next()
// Print each number

while(iterator.hasNext()){
Integer num = iterator.next();
System.out.println(num);
}

System.out.println("\nRemoving odd numbers using Iterator");

Expand All @@ -35,7 +38,12 @@ public static void main(String[] args) {
// TODO:
// Use iterator to remove odd numbers
// Remember: use iterator.remove()

while(iterator.hasNext()){
Integer num = iterator.next();
if(num % 2 !=0 ){
iterator.remove();
}
}

System.out.println("\nUpdated list:");
System.out.println(numbers);
Expand Down
Loading