Results 21 to 27 of 27
 12222009, 04:05 AM #21Member
 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; 12222009 at 04:22 AM.
 12222009, 09:36 AM #22
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 not found, add it to the back 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]); } } }
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
 12222009, 10:06 AM #23
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,316
 Blog Entries
 7
 Rep Power
 25
 12222009, 09:33 PM #24
 12032013, 03:15 PM #25Member
 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class NumberRepetition {
public static void main(String[] args) throws Exception {
int size;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter size of array");
size=Integer.parseInt(br.readLine());
int el;
int[] a=new int[size];
for(int i=0;i<size;i++)
{
System.out.println("enter a number");
el=Integer.parseInt(br.readLine());
a[i]=el;
}
for(int i=0;i<size;i++)
{
for(int j=0;j<size1;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+count1;
}
count=0;
}
}
}
 12032013, 03:22 PM #26Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 5,114
 Rep Power
 9
 12032013, 03:55 PM #27Moderator
 Join Date
 Apr 2009
 Posts
 12,935
 Rep Power
 22
Similar Threads

printing number of pos/neg numbers input?
By shroomiin in forum New To JavaReplies: 12Last Post: 09252009, 02:15 AM 
Prime Number  System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04262009, 01:50 AM 
Compare two lists of number  what numbers arent there
By Bishop609 in forum New To JavaReplies: 5Last Post: 02182009, 02:22 AM 
rounding a double in a number of significant numbers
By rikribbers in forum Advanced JavaReplies: 2Last Post: 10272008, 04:35 PM 
trying to add up random numbers into one number
By pjr5043 in forum New To JavaReplies: 4Last Post: 09152008, 02:20 PM
Bookmarks