- It’s important that students gain hands-on experience manipulating data structures so they learn how to handle the common pitfalls.
- This experience helps to build competency and fluency so students can successfully manipulate data structures in the future.
- Ask students to write codes for test cases.
- You can split students into groups and assign one test case per group.
- You can assign each student to write all test cases.
- You can work as a class to collectively write all the test cases.
- Choose the data structures that best meets your course content needs.
- The example below uses an array.
- The eight test cases (i.e., boundary conditions) students need to consider are as follows:
- Ending with a non-duplicate
- Starting with a duplicate
- Duplicates only in the middle
- A array of one element
- A longer array with all duplicates
- A longer array with no duplicates
- An empty array
- A null array is not possible.
- A single element between sequences of duplicates
- For example, the array [2, 3, 55, 3, 3, 16, 16, 16] is an array with duplicates at the end and in the middle.
- Note from the CS Teaching Tips Team: for more on how to get you class working on boundary test cases check out this tip.
- Extend this assignment by adding on a refactoring activity:
- Gather all the code in once place.
- Have students refactor the gathered test cases into a single method.
- The goal is to end with a single algorithm that can be used to remove duplicates in arrays efficiently and effectively.
- Note: If you assigned each group one test case, they may have inadvertently created test cases that deal with more than the case they were assigned.
-
- Use this as a teaching opportunity and a talking point for group discussion.
- Solutions should look similar to one of the following sample solution methods:
- Sample Method 1:
public void removeAdjacentDuplicates ( ) { int k = 0; while (k < myCount) { if (k < myCount-1 && myValues[k] == myValues[k+1]) { remove (k+1); // it updates myCount } else { k = k+1; } } return this; }
- Sample Method 2:
public void removeAdjacentDuplicates ( ) { int k = 0; while (k+1 < myCount) { if (myValues[k] == myValues[k+1]) { remove (k+1); // it updates myCount } else { k = k+1; } } return this; }
- Sample Method 3:
public void removeAdjacentDuplicates ( ) { for (int i = 0; i+1 < myCount; i++){ if (myValues[i] == myValues[i+1]) { remove (i+1); // it updates myCount i--; } } return this; }
- Sample Method 1: