1

I'm implementing algorithm which will count the appearance of unique numbers in linked list. The problem is in the for loop in which I'm counting the appearance, when the input is something like 1 1 1 2 2 2

Instead of getting for output

1 appearance 3 times
2 appearance 3 times

I'm getting

1 appearance 3
0 appearance 0

Wiht the input 1 2 3 4 5 6 7 8 9 I got array out of index exception. Here's the code

public static void main(String[] args) {
    LinkedList<Integer> list = new LinkedList<Integer>();
    Scanner user_input = new Scanner(System.in);
    String input = user_input.nextLine();
    //Getting user input, if the user enter an empty line(enter, enter) the loop will die
    while(input.length() > 0){
        list.add(Integer.parseInt(input));
        input = user_input.nextLine();
    }
    Collections.sort(list); //sorting the input
    //Getting the number of unique numbers
    int count_of_unique_numbers = 1;// There will be atleast one unique number

    if(!list.isEmpty()){
        int temp = list.get(0);
        for(int i = 1;i < list.size(); i++){
            if(temp != list.get(i)){
                count_of_unique_numbers++;
                temp = list.get(i);
            }
        }
    }
    else{
        System.out.println("The list is empty"); 
        return;
    } 
    //Counting how many times the unique numbers apper;
    int number_appearance[][] = new int[count_of_unique_numbers][2];
    int temp = list.get(0);
    int counter = 1;
    int j = 0;
    for(int i = 1;i < list.size();i++){
        if(temp == list.get(i)){ counter++; }
        else{
            number_appearance[j][0] = temp;
            number_appearance[j][1] = counter;
            counter = 1;
            temp = list.get(i);
            j++;
        }
    }
    //Printing the number_appearance array
    for(int i = 0; i < count_of_unique_numbers; i++){
        System.out.println("The number: " + number_appearance[i][0] + " appearece " + number_appearance[i][1] + " times");
    }
}
5
  • Where are you getting the exception, and have you debugged through it? Commented Jan 14, 2015 at 16:26
  • (You should also consider that you never write the final number into your list of appearances, because you only write that when you spot a different number.) Commented Jan 14, 2015 at 16:27
  • In the third for loop, in the if condition when I try to get the ith element [ temp == list.get(i) ] Commented Jan 14, 2015 at 16:27
  • How do you know that "there will be atleast one unique number" before checking your list for being empty? Commented Jan 14, 2015 at 16:29
  • I check it look at the code. Commented Jan 14, 2015 at 16:33

4 Answers 4

1

You could simply all of that down to this.

Map < String, Integer > numMap = new HashMap < String, Integer > ();
Scanner user_input = new Scanner(System. in );
String input = user_input.nextLine();

String[] inputArray = input.split(" ");
for (String s: inputArray) {
    if (numMap.containsKey(s)) {
        numMap.put(s, numMap.get(s) + 1);
    } else {
        numMap.put(s, 1);
    }
}

for (Map.Entry < String, Integer > entry: numMap.entrySet()) {
    System.out.println("number: " + entry.getKey() + " appeared: " + entry.getValue() + " times");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Well, if you change the key of that Map to type String, then you can avoid the conversion to integer and you don't need to care if the user really entered a number or not.
Very true, i have updated the answer to reflect that. Thank you.
0

You can fix the problem by adding the number of occurences of the final number, which you currently miss :

for(int i = 1;i < list.size();i++){
    if(temp == list.get(i)){ counter++; }
    else{
        System.out.println(temp+":"+counter);
        number_appearance[j][0] = temp;
        number_appearance[j][1] = counter;
        counter = 1;
        temp = list.get(i);
        j++;
    }
}
number_appearance[j][0] = temp; // added
number_appearance[j][1] = counter; // added

Input :

1 1 1 2 2 2

Output :

The number: 1 appearece 3 times
The number: 2 appearece 3 times

Input :

1 2 3 4 5 6 7 8 9

Output :

The number: 1 appearece 1 times
The number: 2 appearece 1 times
The number: 3 appearece 1 times
The number: 4 appearece 1 times
The number: 5 appearece 1 times
The number: 6 appearece 1 times
The number: 7 appearece 1 times
The number: 8 appearece 1 times
The number: 9 appearece 1 times

Comments

0

You could simply do

List<Integer> foundNumbers = new ArrayList<Integer>();
for(int i = 0; i < list.size(); i++)
{
    if(foundNumbers.contains(list.get(i))
    {
        System.out.println(list.get(i) + " appearance " + Collections.frequency(list, list.get(i) + " times.");
        foundNumbers.add(list.get(i));
    }
}

Note that Collections.frequency is available since JDK 1.6

1 Comment

This is exercices i don't need it for any software. And i want to make the algorithm myself. But thanks for your help.
0

I know it's not what you originally were trying to do, but it would be easier to do it using Map

public static void main(String[] args) {
    LinkedList<Integer> list = new LinkedList<Integer>();
    Scanner user_input = new Scanner(System.in);
    String input = user_input.nextLine();
    // Getting user input, if the user enter an empty line(enter, enter) the loop will die
    while (input.length() > 0) {
        list.add(Integer.parseInt(input));
        input = user_input.nextLine();
    }
    Collections.sort(list); // sorting the input
    Map<Integer, Integer> count = new HashMap<Integer, Integer>();
    for (int i : list) {
        if (count.containsKey(i)) {
            count.put(i, count.get(i) + 1);
        }
        else {
            count.put(i, 1);
        }
    }

    for (Map.Entry<Integer, Integer> num : count.entrySet()) {
        System.out.println("The number: " + num.getKey() + " appearece " + num.getValue() + " times");
    }
}

2 Comments

I'm still unfamiliar with maps. But thank you I will keep this piece of code until I read the theory about them.
@austinwernli Not the same, you corrected some spelling errors :)

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.