I have a program that will ask the user to enter 10 elements into an array and then ask them to delete one of them. I have written the classes to delete one instance of the element and that works. Now I have to write one that will delete all instances of the element, meaning that if there are duplicates I need them all deleted. I am stumped. Everything that I have tried is not getting the job done. Can someone please nudge me in the right direction?

To give you an idea of where I am starting from I have included a few classes I have written to remove a single instance of an element from the array.

template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
	if(location < 0 || location >= length)
    	cerr<<"The location of the item to be removed "
			<<"is out of range."<<endl;
	else
	{
   		for(int i = location; i < length - 1; i++)
	 		list[i] = list[i+1];

		length--;
	}
} //end removeAt

template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item)
{
	int loc;
	bool found = false;

	for(loc = 0; loc < length; loc++)
	   if(list[loc] == item)
	   {
		found = true;
		break;
	   }

	if(found)
		return loc;
	else
		return -1;
} //end seqSearch

template <class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem)
{
	int loc;

	if(length == 0)
		cerr<<"Cannot delete from an empty list."<<endl;
	else
	{
		loc = seqSearch(removeItem);

		if(loc != -1)
			removeAt(loc);
		else
			cout<<"The tem to be deleted is not in the list."
				<<endl;
	}

} //end remove

I think it could be done pretty simply like this:

template <class elemType>
void arrayListType<elemType>::removeDups(const elemType& removeItem)
{
	int loc;
    for( loc = 0; loc < length-1; loc++)
    {
        if( list[loc] == removeItem)
        {
            removeAt(loc);
            --loc; // repeat last test because item has moved
       }
   }
} //end remove
commented: Thanks so much for the help! +0

Thank you so much. That worked perfectly. Can you explain the last step (--loc). You commented that it repeats because the item has moved. I don't quite understand that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.