Jump to content

Recommended Posts

Posted

Boa tarde, gostava de saber como faço um metodo recursivo que dada um array elementos do tipo

inteiro que descubra se a sequencia tem algum elemento repetido.

boolean temRepetidos ( int [ ] v )...

Agradecia umas luzes....

Posted
public class testes {
private static boolean contains(int [] v, int target, int first){
	if(first >= v.length)
		return false;
	else if(v[first] == target)
		return true;
	else	
		return contains(v, target, (first + 1));
}

public static boolean cont(int [] v){
	return cont (v,0);


}
public static boolean cont(int [] v, int index){
	if( index + 1 == v.length ){
		return false;
	}
	else if (contains(v,v[index],index +1))
		return true;

	return cont (v , index+1);

}

public static void main (String [] args){

	int v [] = {1,2,3,4,5,8,7,8};
	System.out.println(cont(v));

}
}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.