java - Does break statement make my code faster? -
i supposed write code if array has duplicates. runtime wasn't important. think below code have o(n²)
because used nested for-loop. know there better , faster codes 1 have written, my question if break
statement made inside if-statement make code (a bit) faster? should make faster because program knows "hey, found duplicate , can stop searching more". once heard fellow student code better / more stable if avoid statements return
or break
. bad didn't care enough ask why. maybe can tell me if true?
and if right , these statements "hurt" code, there better workarounds?
public class findduplicate{ public static void main(string[] args){ int[] a={1,2,3,4,5,6,7,8,4}; boolean bool=false; for(int i=0; i<a.length; i++){ for(int j=0; j<a.length; j++){ if(a[i]==a[j] && i!=j){ bool=true; break; } } } if(bool==true){ system.out.print("duplicate found"); }else{ system.out.print("no duplicate found"); } } }
my question if break statement made inside if-statement make code (a bit) faster?
not in every case, however, in cases, make code faster considering don't have keep iterating when you've found you're after.
the algorithm below contains 2 nested loops. outer loop iterates on array’s n
items, takes o(n)
steps. each trip through outer loop, inner loop iterates on n
items in array, takes o(n)
steps. because 1 loop nested inside other, combined performance o(n × n) = o(n2)
.
for(int = 0; < a.length; i++){ for(int j=0; j < a.length; j++){ if(a[i] == a[j] && != j){ bool = true; break; } } }
we can make algorithm bit faster not going j = 0
@ each iteration of outer loop.
for(int = 0; < a.length; i++){ for(int j = i+1; j < a.length; j++){ if(a[i] == a[j]){ bool = true; break; } } }
note in case don't need check && != j
because never equal.
i once heard fellow student code better / more stable if avoid statements return or break
the jvm
specification not state either existence or absence of performance loss when using break
. put simply, there no proof whatsoever using break
or return
makes code unstable(not know of anyway). scenario in "oh not practise" when overuse word break
. however, in many cases break
possibility accomplish task faster, example current solution. basically, why carry on iterating when you've found after?. consider return
not being "a bad practise" because similar break
, why carry on executing code when don't need to, surely makes code faster.
can make find duplicate algorithm faster?
sure can, consider set
interface in java doesn't allow duplicates , it's based upon hash table data structure insertion take o(1)
time in average case. using hashset
, general purpose set
implementation, can find duplicates in o(n)
time. since hashset
allows unique elements, add()
method fail , return false
when try add duplicates.
solution:
public static boolean hasduplicate(int[] array) { set<integer> dupes = new hashset<integer>(); (integer : array) { if (!dupes.add(i)) { return true; // have found duplicate } } return false; // no duplicate }
Comments
Post a Comment