0

I made a pseudo code algorithm to find repeating values of an array which includes float numbers:

mergeSort(A);  
int i <- 0
for i<- A.lenght-1
  if Arr[i] == A[i+1]
          return A[i]
          while A[i] = A[i+1]
                i++
else
    i++

I want to change the above algorithm to find the repeating values and the number of times they repeat. I have created the following algorithm:

mergeSort(A); 
HashMap hashMap;
Int result <-0 int i <- 0
for 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 <- i+1
             j<- j+1
         hashMap.insert(result , j)
      else
         i++
return hashMap

Is this an efficient algorithm? Is it a good way to use a hashmap?

3 Answers 3

1

You can simply use HashMap and do something like this:

HashMap hash;
for (int i = 0; i < A.lenght; i++){
    value = hash.get(A[i]); 
    if (value == null) // is the first time that we find A[i]
        hash.put(A[i], 1);
    else    // A[i] is a duplicate
        hash.put(A[i], value + 1);
}

avarage case = O(n)

I agree with pboulanger: pay attention to floating-point comparison.

Sign up to request clarification or add additional context in comments.

3 Comments

thats great, thanks. but, does hashmap accept float as a key? isnt there ant constraint for hashmap only to accept integers as keys?
does data structure HashTable has the method get(key)- that you have use above - too?
Sandra you can use float, but you have to use wrapper class. Here are all methods supported download.oracle.com/javase/1.4.2/docs/api/java/util/… . Anywhere if you know how HashMap works, you can implement your own HashMap.
1

Be careful, the floating-point numbers are not exact types and the equal operator could return wrong values. For floating-point values prefer a test |f1-f2|

Comments

0

Another option would be to store the result in an ArrayList of [value count] pairs, this is probably slightly more effective, there's really no need for a HashMap here. I know practially nothing about java, but you should be able to write something like:

arrayList.add(new Pair(result, j));

6 Comments

How can I insert pairs into ArrayList? arrayList arr;
how about an HashTable? Isnt it better that I use it?
A hashmap is useful when you need to do fast lookups in a table with "random" indices. What you're doing here is inserting numbers in a well defined order so there's really no need for a hash map.
does it differ if I want to find duplicate i an array which includes integer numbers? should I use arayList for integers too?
@Andreas Brinck, Array List is not efficient in all, It's better to use list or array or other generic collections not array list.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.