0

So I'm trying to make a program that counts the occurences of int in an array. what i tried to do is to make a method that lists the unique integers, then another method to compare the list items to the original array items.

public List listUnique(int[] arr){
    Arrays.sort(arr);
    List <Integer> temp = new ArrayList<>();
    int currentInt = 0;
    for (int i = 0; i < arr.length; i++){
        if(arr[i] != currentInt){
            temp.add(arr[i]);
            currentInt = arr[i];
        }
    }
    return temp;
}
public int[] countDupli(List unique, int[] arr){
    int [] ret = new int[unique.size()];
    Iterator <Integer> iterator = unique.iterator();
    for (int l = 0; l < unique.size(); l++){
        ret[l] = iterator.next().intValue(); 
    }   
    int[] dupli = new int[ret.length];
    for (int j = 0; j < ret.length; j++){
        dupli[j] = 0;
    }
    for (int k = 0; k < ret.length; k++){
        for (int i = 0; i < arr.length; i++){
            if ( ret[k] == arr[i]){
                dupli[k]+= 1;
            }
        }
        k++;
    }
return dupli;
}

It isn't doing what is intended to do tho. For example, an input of {1,2,...,1,2} 10 items of those, prints the correct unique items but only outputs the count of 1 but not 2. dupli = [5,0]. where did the algorithm go wrong? thanks

5
  • Did you try debugging? Commented Mar 8, 2017 at 23:47
  • not yet, sorry, i'm quite a newbie in java. Commented Mar 8, 2017 at 23:50
  • sounds like a job for a map Commented Mar 8, 2017 at 23:52
  • 1
    This is the perfect opportunity to look into it. Debugging gives you the tools to understand your own code, which you're in a much better position to do than anyone else. Commented Mar 8, 2017 at 23:52
  • Thank you everyone, I found that I incremented my k-index twice in a row. my bad. thanks for the help. I'll definitely look into debugging. Commented Mar 8, 2017 at 23:57

1 Answer 1

0

In your code try debugging or just print ret[] values and see if all unique values are present.

Other Suggestions:

List <Integer> temp = new ArrayList<>();
        int currentInt = 0;
        for (int i = 0; i < arr.length; i++){
            if(arr[i] != currentInt){
                temp.add(arr[i]);
                currentInt = arr[i];
            }

You can simply use HashSet, then you don't have to write above code to find unique values. HashSet does not store duplicate values.

 Set<Integer> set = new HashSet<Integer>(Arrays.asList(arr));

Then you can use iterator and compare with your sorted array and increase count simultaneously.

You can skip the following for loop, as java initializes it to 0 by default.

    int[] dupli = new int[ret.length];
    for (int j = 0; j < ret.length; j++){
        dupli[j] = 0;
    }
Sign up to request clarification or add additional context in comments.

Comments

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.