logo Practice-It logo


Related Links:
Author: Marty Stepp (on 2012/02/29)

Write a method countDuplicates that returns the number of duplicates in a sorted list. The list will be in sorted order, so all of the duplicates will be grouped together. For example, if a variable list stores the sequence of values below, the call of list.countDuplicates() should return 7 because there are 2 duplicates of A, 1 duplicate of B, 1 duplicate of K, 2 duplicates of W and 1 duplicate of Z:

[A, A, A, B, B, E, G, K, K, W, W, W, Z, Z]

Remember that you may assume that the list is in sorted order, so any duplicates would occur consecutively. You may assume that no element in the list is null.

The example shown is a list of strings, but this is a generic linked list class that can store any type of objects. You should take this into account in your code. Assume that you are adding this method to the LinkedList<E> class as defined below:

public class LinkedList<E> {
    private ListNode front;   // null for an empty list
Type your solution here:

This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.