creating an algorithm to find repeating values of an array -


i made pseudo code algorithm find repeating values of array includes float numbers:

mergesort(a);   int <- 0 i<- a.lenght-1   if arr[i] == a[i+1]           return a[i]           while a[i] = a[i+1]                 i++ else     i++ 

i want change above algorithm find repeating values , number of times repeat. have created following algorithm:

mergesort(a);  hashmap hashmap; int result <-0 int <- 0 i<- a.lenght-1     int j <- 0     if a[i] == a[i+1]           j <- j+1           result <- a[i]           while a[i] == a[i+1]              <- i+1              j<- j+1          hashmap.insert(result , j)       else          i++ return hashmap 

is efficient algorithm? way use hashmap?

you can use hashmap , this:

hashmap hash; (int = 0; < a.lenght; i++){     value = hash.get(a[i]);      if (value == null) // first time find a[i]         hash.put(a[i], 1);     else    // a[i] duplicate         hash.put(a[i], value + 1); } 

avarage case = o(n)

i agree pboulanger: pay attention floating-point comparison.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -