Algorithm to find duplicate in an array -
i have assignment create algorithm find duplicates in array includes number values. has not said kind of numbers, integers or floats. have written following pseudocode:
findingduplicatealgorithm(a) // array mergesort(a); int <- 0 i<a.length if a[i] == a[i+1] i++ return a[i] else i++
have created efficient algorithm? think there problem in algorithm, returns duplicate numbers several time. example if array include 2 in 2 two indexes have ...2, 2,... in output. how can change return each duplicat 1 time? think algorithm integers, work float numbers too?
to handle duplicates, can following:
if a[i] == a[i+1]: result.append(a[i]) # collect found duplicates in list while a[i] == a[i+1]: # skip entire range of duplicates i++ # until new value found
Comments
Post a Comment