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