algorithm - Is there a way to loop over an integer array, keeping track of all unique elements WITHOUT allocating a new array? -
an array contains n elements, each element within range 1 <= k <= x x <= n. elements in no particular order, there many duplicate elements. wondering if there way iterate on array, stopping 1 of every integer 1-x has been seen, , doing without allocating additional array , without sorting original array.
edit: array guaranteed contain values 1-x. x parameter, not constant.
edit:
more generally, wondering if there known way of combining unique consecutive integers sum or product, such value of sum or product can tell whether or not 1 of integers part of it. multiplying unique primes together.
thanks
there trivial way, exceptionally inefficient. use 2 nested loops. untested pseudocode:
lastindex = 0; containsall = true; ( i=1; <= x; ++ ) { ( j=0; j < n; j++ ) { if ( array(j) == ) { if ( j > lastindex ) { lastindex = j; } break; } } }
Comments
Post a Comment