If you do not like sorting, then this solution solves it without sorting. You should be able to make it much more efficient.
boolean hasRepeatedElements(int[] v)
{
if ( v . length <= 1 )
{
return ( false ) ;
}
int k = RANDOM . nextInt ( v . length ) ;
int leftLength = v . length / 2 ;
int [ ] left = new int [ leftLength ] ;
int rightLength = v . length - l ;
int [ ] rightLength = new int [ rightLength ] ;
int i , l , r ;
for ( i = 0 , l = 0 , r = 0 ; i < v . length ; i ++ )
{
if ( ( v [ i ] < v [ k ] ) & ( l < leftLength ) )
{
left [ l ] = v [ i ] ;
l ++ ;
}
else if ( ( v [ i ] >= v [ k ] ) & ( r < rightLength ) )
{
right [ r ] = v [ i ] ;
r ++ ;
}
else
{
return ( hasRepeatedElements ( v ) ) ;
}
}
if ( hasRepeatedElements ( left ) ) { return ( true ) ; }
if ( hasRepeatedElements ( right ) ) { return ( true ) ; }
return ( false ) ;
}
This solution is not very efficient, but it uses recursion and it follows the method signature exactly.
static java . util . Random RANDOM = new java . util . Random ( ) ;
boolean hasRepeatedElements(int[] v)
{
int s = Random . nextInt ( v . length ) ;
int t = Random . nextInt ( v . length ) ;
int temp = v[s] ;
v[s] = v[t] ;
v[t] = temp ;
for ( int i = 0 ; i < v . length - 1 ; i ++ )
{
if ( v[i]==v[i+1] ) { return true ; }
else if ( v[i]>v[i+1] { return hasRepeatedElements ( v ) ; }
}
return false ;
}