Is this a simple way to return the number of occurrences of theValue in a bag?

/**
 * Return the number of occurrences of theValue in the bag.
 */
public int countOccurrences( String theValue ) {

	// fill in code 
	 for(int index = 0; index <= topPosition; index++ )
			if(contents[index] == theValue) 
				return index;

Recommended Answers

All 2 Replies

This is the new piece of code I have

/**
 * Return the number of occurrences of theValue in the bag.
 */
public int countOccurrences( String theValue ) {

// fill in code 
	int count = 0; 
	  
	for(int i = 0;i < theValue.length();i++) { 
	if(theValue.equals(contents)){ 
	    count++; 
	}  
	} 
	return count; 
	}

You need to provide more information as what are those variables that you have?
Do you have an array and you want to see how many times the argument is inside the array?
Then loop the array and take each element of the array and compare it with the argument

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.