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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/sQ9gPiKY)
41 changes: 36 additions & 5 deletions app/src/main/java/assignment/Set.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
package assignment;

public class Set<T> {
public void add(T item) {}
private T[] set = (T[]) new Object[32];

public boolean contains(T item) {
public void add(T item) { // O(n)
int firstFreeSpot = -1; // O(1)
for (int i = 0; i < set.length; i++) { // O(n)
if (set[i] == item) { // O(1)
return;
}
if (set[i] == null && firstFreeSpot == -1) { // O(1)
firstFreeSpot = i; // O(1)
}
}
set[firstFreeSpot] = item; // O(1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an edge case the unit tests don't cover and slightly outside the scope of this assignment, but as an exercise, what would happen if the client attempts to add() a new element and the set is already full? What are some reasonable strategies you see to handle such a situation gracefully?

}

public boolean contains(T item) { // O(n)
for (T currentItem : set) { // O(n)
if (currentItem == item) { // O(1)
return true;
}
}
return false;
}

public void remove(T item) {}
public void remove(T item) { // O(n)
for (int i = 0; i < set.length; i++) { // O(n)
if (set[i] == item) { // O(1)
set[i] = null; // O(1)
return;
}
}
}

public int size() {
return 0;
public int size() { // O(n)
int nullCount = 0; // O(1)
for (int i = 0; i < set.length; i++) { // O(n)
if (set[i] == null) { // O(1)
nullCount++; // O(1)
}
}
return set.length - nullCount;
}
}
Loading