# Thread: how to do write this method with more efficiency ?

1. Member
Join Date
Sep 2012
Posts
4
Rep Power
0

## 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 ?

Java 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"```

3. ## Re: how to do write this method with more efficiency ?

Originally Posted by Norm
Not very efficient, that ;)

db

4. ## Re: how to do write this method with more efficiency ?

5. Member
Join Date
Mar 2012
Location
Novosibirsk, Russia
Posts
15
Rep Power
0

## 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]).

6. ## 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

7. Member
Join Date
Oct 2012
Posts
5
Rep Power
0

## Re: how to do write this method with more efficiency ?

i dont know if better than yours . i m a newbie too

try this

Java 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;
}```

8. Member
Join Date
Oct 2012
Posts
5
Rep Power
0

## Re: how to do write this method with more efficiency ?

and efficiently
Java 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;
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•