2

I am trying to find the occurrences of all integers submitted by the user into the program and so far here's what I got. It works but I don't think it's a legit way to do it, is there any way I can try to do this without using the list[10]=9999;? Because if I don't do it, it'll show out of boundary error.

import java.util.*;

public class OccurrencesCount
{
   public static void main(String[] args) 
   {
   Scanner scan = new Scanner(System.in);

  int[] list = new int[11];

  //Accepting input.
  System.out.print("Enter 10 integers between 1 and 100: ");
  for(int i = 0;i<10;i++){
     list[i] = scan.nextInt();
     list[10] = 9999;
  }
  //Sort out the array.
  Arrays.sort(list);
  int count = 1;
  for(int i = 1;i<11;i++){
        if(list[i-1]==list[i]){
           count++;
        }
        else{
           if(count<=1){
              System.out.println(list[i-1] + " occurs 1 time.");
           }
           else{
              System.out.println(list[i-1] + " occurrs " + count + " times.");
              count = 1;
           }
        }
     }

}
}
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
T. Hry
  • 23
  • 5
  • 1
    Is there a reason the size of your int array is 11, but you only loop through 10 times in the for loop? – Brandon Laidig Nov 04 '15 at 00:38
  • 1
    What does setting list[10] = 9999 get you? – jeff carey Nov 04 '15 at 00:40
  • 1
    possible duplicate http://stackoverflow.com/questions/33459302/counting-occurrences-of-integers-in-an-array, and http://stackoverflow.com/questions/8098601/java-count-occurrence-of-each-item-in-an-array – tam5 Nov 04 '15 at 00:51
  • I had to put in an extra value in the array so that when it comes to the if statement below, I won't get an out of boundary error statement – T. Hry Nov 04 '15 at 02:41

4 Answers4

0
import java.util.*;
class OccurrencesCount {
   public static void main(String[] args) {
   Scanner scan = new Scanner(System.in);
   int[] list = new int[101];
   System.out.print("Enter 10 integers between 1 and 100: ");
   for(int i = 0;i<10;i++) {
     int x = scan.nextInt();
     list[x]++;
  }
  for(int i = 1; i <= 100; i++) 
     if(list[i] != 0)
       System.out.println(i + " occurs " + list[i] + " times ");

    }
}

To store count of numbers from 1 to 100 you need to use a list of 100 integers each one storing the number of occurrences of itself. Better approach would be to use a Map.

0

Personally, I think this is a perfectly good solution. It means that you can deal with the last group in the same way as all others. The only change I would make is putting the line list[10] = 9999; outside the for loop (there's no reason to do it 10 times).

However, if you want to use an array of length 10, you can change the line

if(list[i-1]==list[i])

to

if(i < 10 && list[i-1]==list[i])

If you do this, you won't get an ArrayIndexOutOfBoundsException from list[i] because the expression after the && is not evaluated when i == 10.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • I tried adding the i < 10 and deleting the list[10] but it ends up counting the 0 at first as 1, so I tried 9 instead; now 9 doesn't work but it's very close to being correct, only that it doesn't check the value of the last integer in the sorted array? (and yes they only wanted an array of 10) – T. Hry Nov 04 '15 at 02:47
0

I have made use of ArrayList and Collections package instead of regular arrays as a different flavor if it interests you check it out.

import java.util.*;

public class OccurrencesCount {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        List<Integer> list = new ArrayList<>();

        //Accepting input.
        System.out.print("Enter 10 integers between 1 and 100: ");
        for (int i = 0; i < 10; i++) {
            list.add(scan.nextInt());
        }

        Collections.sort(list);
        Integer prevNumber = null;
        for (int number : list) {
            if (prevNumber == null || prevNumber != number) {
                int count = Collections.frequency(list, number);
                System.out.println(number + " occurs " + count + (count > 1 ? " times." : " time."));
            }
            prevNumber = number;
        }
    }
}
Sachin
  • 901
  • 2
  • 10
  • 23
0

I got it figured out guys, only changed a couple of small things inside the conditions and everything went smoothly! Thanks for the help! Now I just need to find a way to restrict the inputs from going above 100.

import java.util.*;
 public class CountOccurrences
 {
 public static void main(String[] args) 
 {
  Scanner scan = new Scanner(System.in);

  int[] list = new int[10];

  //Accepting input.
  System.out.print("Enter 10 integers between 1 and 100: ");
  for(int i = 0;i<=9;i++){
     list[i] = scan.nextInt();
  }
  //Sort out the array.
  Arrays.sort(list);
  int count = 1;
  for(int i = 1;i<=10;i++){
        if(i<=9 && list[i-1]==list[i]){
           count++;
        }
        else{
           if(count<=1){
              System.out.println(list[i-1] + " occurs 1 time.");
           }
           else{
              System.out.println(list[i-1] + " occurrs " + count + " times.");
              count = 1;
           }
        }
     }

 }
}
T. Hry
  • 23
  • 5