# how to do write this method with more efficiency ?

• 10-01-2012, 03:40 PM
romavolman
how to do write this method with more efficiency ?
The program below accept two array with hole pozitive nums, with same size N, and fill empty array "c" with nums of "a" that do not exist in "b" and return the biggest num of "a" that do not exist in "b".
qustion: how to do write this method with more efficiency ?

Code:

public int f(int[]a, int[]b, int[] c)
{
int N = a.length;
int j, max = 0, g = 0, t = 0;

for(int i=0; i < N; i++)
{
for(j=0; j<N; j++)
if(b[j] == a[i]) // if find the num in "b" go to the next num in "a"
break;

if(j == N)
{
c[t] =a[i]  // fill empty array "c" with nums of "a" that do not exist in "b"
if(g ==0 || c[t] > max)
{
max = c[t]; // remmber the max num in "max"
g = 1;

}
t++;

}

}
return max; // max of "a" that you do not have in "b"

• 10-01-2012, 04:38 PM
Norm
Re: how to do write this method with more efficiency ?
• 10-01-2012, 06:36 PM
DarrylBurke
Re: how to do write this method with more efficiency ?
Quote:

Originally Posted by Norm

Not very efficient, that ;)

db
• 10-01-2012, 06:38 PM
DarrylBurke
Re: how to do write this method with more efficiency ?
• 10-03-2012, 12:29 PM
rfq
Re: how to do write this method with more efficiency ?
create HashSet<Integer>bs containing all values from the array b[], and check existence with bs.contains(a[i]).
• 10-03-2012, 10:28 PM
kjkrum
Re: how to do write this method with more efficiency ?
First you need to know how you will know when you have answered your question. You need to characterize how your algorithm currently performs in order to know how you might improve it. Here's a good place to start: A Beginner’s Guide to Big O Notation « Rob Bell
• 10-09-2012, 05:11 PM
aljassi
Re: how to do write this method with more efficiency ?
i dont know if better than yours . i m a newbie too

try this

Code:

private double returnBiggestElement( double[]a,double[]b, double[]c){
double max=0;
for(int i=0;i<a.length;i++){
for(int j=0;j<b.length;j++){
if(a[i]!=b[j]){
c[i]=a[i];
if(c[i]>max){
max=c[i];
}
}
}
}

return max;
}

• 10-09-2012, 05:17 PM
aljassi
Re: how to do write this method with more efficiency ?
and efficiently
Code:

private double returnBiggestElement( double[]a,double[]b){
double max=0;
for(int i=0;i<a.length;i++){
for(int j=0;j<b.length;j++){
if(a[i]!=b[j]&& a[i]>max){
max=a[i];
}
}
}

return max;
}