# Thread: inputs numbers then outputs how many time a particular number appears

1. Member
Join Date
Feb 2009
Posts
92
Rep Power
0
I'm hardly the one to say, but go ahead; I agree with you and we would all be interested at this point.

Besides, wtd nielson has already posted a working solution that is simple and elegant. Just ran it. Should have done that much sooner. My bad and apologies.

Note that that any duplicates will be adjacent in a sorted list. This is the key to his solution which works in one pass.

Any differences would be of interest, whether as a correct model, or one to avoid.
Last edited by rdtindsm; 12-22-2009 at 04:22 AM.

2. Okay then. Here's my solution. I tried to keep it simple and there's much room for improvement. ;)
Java Code:
```import java.util.*;

public class Main {
public static void main(String[] args) {
// program parameters
boolean loop = true;
Scanner scanner = new Scanner(System.in);
// an array based map
int[] numbers = new int[100];
int[] counters = new int[100];
int count = 0;
// get the input
while (loop) {
System.out.println("Enter a number or enter -999 to continue: ");
int next = scanner.nextInt();
loop = next != -999;
if (loop) {
int pos = 0;
boolean found = false;
// search for number and increment count
for (int i = 0; i < count; i++) {
if (numbers[i] == next) {
counters[i]++;
found = true;
break;
}
}
if (!found) {
numbers[count] = next;
counters[count] = 1;
count++;
}
}
}
// sort the numbers with the bubble sort
boolean swapped = true;
while (swapped) {
swapped = false;
for (int i = 0; i < count - 1; i++)  {
if (numbers[i + 1] < numbers[i]) {
swapped = true;
int temp = 0;
// swap the number
temp = numbers[i + 1];
numbers[i + 1] = numbers[i];
numbers[i] = temp;
// swap the counter
temp = counters[i + 1];
counters[i + 1] = counters[i];
counters[i] = temp;
}
}
}
// output the map
for (int i = 0; i < count; i++) {
System.out.println(numbers[i] + "_____" + counters[i]);
}
}
}```
Hope this adds to the discussion. :)

3. Originally Posted by tim
Hope this adds to the discussion. :)
The OP will be very grateful, copies and pastes your code and turns it in as if it were his own.

kind regards,

Jos

4. Originally Posted by tim
Can I post the solution? Others do not agree with this, but I feel that students learn best by example. ;)
Originally Posted by JosAH
The OP will be very grateful, copies and pastes your code and turns it in as if it were his own.
If he plagiarizes, then he steals from himself. :rolleyes:

5. Member
Join Date
Dec 2013
Posts
1
Rep Power
0

## Re: inputs numbers then outputs how many time a particular number appears

[QUOTE=koji_kun;96119]Ok, this is what my teacher wants me to do:

Write a program that reads a set of positive integers and outputs how many times a particular number appears in the list. You may assume that the data set has at most 100 numbers and -999 marks the end of the input data. The numbers must be output in increasing order. For example, for the data:

15 40 28 62 95 15 29 13 62 65 48 95 62 65 95 95 -999

The output is:
Number Count
13_____1
15_____2
28_____2
40_____1
48_____1
62_____3
65_____3
95_____4

public class NumberRepetition {

public static void main(String[] args) throws Exception {
int size;
System.out.println("enter size of array");
int el;
int[] a=new int[size];
for(int i=0;i<size;i++)
{
System.out.println("enter a number");
a[i]=el;
}
for(int i=0;i<size;i++)
{
for(int j=0;j<size-1;j++)
{
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;

}
}
}
int count=0;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(a[i]==a[j])
{
count++;

}

}
System.out.println(a[i]+" \t " +"occurence time is"+"\t"+count);
if(count!=0)
{
i=i+count-1;
}
count=0;
}

}

}

6. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
2,687
Rep Power
3

## Re: inputs numbers then outputs how many time a particular number appears

Originally Posted by tim
Can I post the solution? Others do not agree with this, but I feel that students learn best by example. ;)
I feel that students are piss-poor at actually learning something and look for "examples" to work around that.

7. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: inputs numbers then outputs how many time a particular number appears

The post before yours seem to have resurrected a 4 year old thread.
I think I will put it to sleep.

Page 2 of 2 First 12

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•