# Your input with written code (Sorting and searching within array)

• January 9th, 2012, 01:14 PM
GalBenH
Your input with written code (Sorting and searching within array)
So here's the assignment I was given:
Writing a method that receives an int array titled values.
the method has to return True, if throughout the array, there's an int that exists only once, or return false otherwise.
Example : for an array 3,1,4,1,4,1 the method returns True
for an array 3,1,4,1,4,3 method returns false.
The received array is not sorted

Here's my code

Code :

```public boolean single(int[] values) { boolean existsOnce = false;   if(values.length == 1) return existsOnce = true; quicksort(values,0,(values.length -1));   int counter = 0; int tempNumber = values[0]; for(int i =0; i < values.length; ) { if(tempNumber == values[i]){ i++; counter ++; } else{ if(counter == 1 || i == (values.length -1) ) return existsOnce = true; else { tempNumber = values[i]; counter= 0; } }   } return existsOnce ; }```

Do you guys see any way of making this more efficient. also what would the "Big O" be? The class was about efficiency and that's what this method is supposed to strive for.

Thanks!

Gal
• January 9th, 2012, 01:20 PM
KevinWorkman
Re: Your input with written code (Sorting and searching within array)
Some words to the wise:

Use indentation.
Use brackets on every if and for, even if it's just one statement.
Why: return existsonce = true;

What do you think the "Big O" would be? Have you input different values of n to see how many iterations each takes?
• January 9th, 2012, 01:24 PM
newbie
Re: Your input with written code (Sorting and searching within array)
//Removed some - as to not void Kevin's post
An easier solution might be to used the Arrays utility class.
• January 9th, 2012, 02:32 PM
copeg
Re: Your input with written code (Sorting and searching within array)
For what its worth, I do not see anywhere in the requirements that says the array should be sorted (yes, this is a hint in more ways than one)
• January 9th, 2012, 03:11 PM
GalBenH
Re: Your input with written code (Sorting and searching within array)
Thanks all for your prompt help!
Kevin,
Regarding the existsOnce=true
Before getting into any loop I wanted to eliminate the scenario of having an Array of only 1 int, which by deafult need this method to return True (for a single appearance of a specific int)

newbie,
I'm not supposed to use that as a part of the requirements.

Copeg,

That's an interesting approach, I was taking it for granted that a sorted array may make things simpler but it's definitely not a requirement.

What do you think of this:

Code :

```public boolean single(int[] values) { boolean existsOnce = false; if(values.length == 1) return existsOnce = true;   int counter = 0; int tempNumber = values[0]; for(int i=0; i<values.length;i++){ if(tempNumber == values[i]){ counter ++;} else{ if(counter == 1 || i == (values.length -1) ) return existsOnce = true; else{ tempNumber = values[i]; counter= 0;} } } return existsOnce ; }```

my main concern is that I'm not sure in this line:

for(int i=0; i<values.length;i++){
if(tempNumber == values[i]){
counter ++;}

If the tempNumber is progressed in ever step like the i is, in order to compare it the the remaining rest of the array in each step of the loop.

I would love to see a sample correct solution from you guys (if possible) without making things too complicated (not supposed to be using advanced material).
I actually learn a lot from these answers so please don't see it as a way of me trying to find an easy way out.
My mind is drawing blank right now
• January 9th, 2012, 03:49 PM
Norm
Re: Your input with written code (Sorting and searching within array)
Quote:

counter ++;}
Another comment on coding standards.
Do NOT hide } on the same line with a statement.
The ending } should be on a line by itself and in the same column as the statement line with the beginning {. That allows your eye to easily see the code structure and scope of variables.
• January 10th, 2012, 07:59 AM
KevinWorkman
Re: Your input with written code (Sorting and searching within array)
Quote:

Originally Posted by GalBenH
Kevin,
Regarding the existsOnce=true
Before getting into any loop I wanted to eliminate the scenario of having an Array of only 1 int, which by deafult need this method to return True (for a single appearance of a specific int)

So why not just return true?

Quote:

Originally Posted by GalBenH
What do you think of this:

Code :

```public boolean single(int[] values) { boolean existsOnce = false; if(values.length == 1) return existsOnce = true;   int counter = 0; int tempNumber = values[0]; for(int i=0; i<values.length;i++){ if(tempNumber == values[i]){ counter ++;} else{ if(counter == 1 || i == (values.length -1) ) return existsOnce = true; else{ tempNumber = values[i]; counter= 0;} } } return existsOnce ; }```

I'm honestly not really sure how that's supposed to solve your problem. Does it?

Quote:

Originally Posted by GalBenH
I would love to see a sample correct solution from you guys (if possible) without making things too complicated (not supposed to be using advanced material).
I actually learn a lot from these answers so please don't see it as a way of me trying to find an easy way out.
My mind is drawing blank right now

That's not really how this works. Does your solution work? If not, step through it with a debugger to see exactly what it's doing. Think about your approach- how would you do this in your head, without a computer? Really examine what you're actually doing in your head instead of saying "well I would just see if there were two numbers that are the same." How would you really do that? What if the list was a thousand numbers? How would you do it in your head or with a piece of paper and a pencil then?
• January 10th, 2012, 05:31 PM
GalBenH
Re: Your input with written code (Sorting and searching within array)
I think I finally got it:

Code :

```public boolean single(int[] values) {   if (values.length == 1) { return true; }     for (int i=0; i<values.length; i++) { int counter = 0; int tempNumber = values[i];   for (int j=0; j<values.length; j++) { if(tempNumber == values[j]) { counter++; }   if (j == values.length && counter == 1) { return true;   } else { return false } } } }```

Do you guys approve?
• January 11th, 2012, 08:04 AM
KevinWorkman
Re: Your input with written code (Sorting and searching within array)
Like I keep hinting at, it doesn't matter whether we approve. Does it do what you want? Do you understand how it works? Then don't worry about it.

Actually, I don't see how this can possibly do what you described. Did you try running it with different inputs?
• January 11th, 2012, 08:19 AM
Norm
Re: Your input with written code (Sorting and searching within array)
Can you explain the logic for your code?
For example:
Why the counter?
When in the search do you know that the condition you are looking for is true?