Counting Number of appearances of a particular number in an array

Hello all-
Here is my problem, I am writing a java program that is supposed to count the number of times each number appears within an array, making sure not to print the same number twice...(i.e. if 2 appears twice the program can't print, "2 appeared 2 times" more than once. I have written a solution to this problem only to discover that I am not allowed to sort the array. So for example if the array looks like this...{1,5,3,2,9,5} the printing has to go in that exact order. It can't print in ascending order (i.e. 1,2,3...etc). Here is my original code....


public static void main(String[] args){

int array1[] = Assignment3Array.getFirstArray();
int array2[] = Assignment3Array.getSecondArray();
int array[] = new int[array1.length + array2.length];
int current;
int cases = 0;
int counter;
int min, temp;


//put the two arrays into one single array
for(int z = 0; z < array1.length; z++){
array[z] = array1[z];
}
for(int x = array1.length, i = 0; x < array.length; x++, i++){
array[x] = array2[i];
}

for(int i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
System.out.println();

/*
Bubble Sort:
This is necasary in order for the printing loops to work properly.
Note that the inner for loop could test to array.length - 1, but
that would cause the last number to not be sorted if it were to equal a number
that was already in the array.
*/

for(int index = 0; index < array.length; index++){
min = index;
for(int test = index+1; test < array.length; test++)
if(array[test] < array[min])
min = test;
temp = array[min];
array[min] = array[index];
array[index] = temp;
}

//test to see if sort works
//for(int z = 0; z < array.length; z++)
//System.out.println(array[z]);

//Count number of cases of each element
for(int i = 0; i < array.length; i++){
cases = 0;
current = array[i];
for(counter = 0; counter < array.length; counter++){
if(array[counter] == current){
cases++;
}
}
//This if is used to ensure that the same number won't print twice
if(i != array.length - 1 && array[i] == array[i+1])
continue;
else
System.out.println(current + " times occured: " + cases);
}
}
}


Any tips as to what I might to differently would help out alot. Thanks for your time.
-BigQ

Comments

  • public class NumberCount
    {
    public void printNonDuplicationNumbers(int[] number)
    {
    for (int i = 0; i < number.length; i++)
    {
    boolean found = false;

    for (int j = 0; j < i - 1; j++)
    {
    if (number[i] == number[j])
    {
    found = true;
    break;
    }
    }

    if (found == false)
    {
    System.out.println(number[i]);
    }
    }
    }

    public static void main(String[] args)
    {
    int array[] = {1,2,6,5,3,7,2,7,5,3, 10};
    NumberCount count = new NumberCount();
    count.printNonDuplicationNumbers(array);
    }
    }

    Regards,
    Muruga

    : Hello all-
    : Here is my problem, I am writing a java program that is supposed to count the number of times each number appears within an array, making sure not to print the same number twice...(i.e. if 2 appears twice the program can't print, "2 appeared 2 times" more than once. I have written a solution to this problem only to discover that I am not allowed to sort the array. So for example if the array looks like this...{1,5,3,2,9,5} the printing has to go in that exact order. It can't print in ascending order (i.e. 1,2,3...etc). Here is my original code....
    :
    :
    : public static void main(String[] args){
    :
    : int array1[] = Assignment3Array.getFirstArray();
    : int array2[] = Assignment3Array.getSecondArray();
    : int array[] = new int[array1.length + array2.length];
    : int current;
    : int cases = 0;
    : int counter;
    : int min, temp;
    :
    :
    : //put the two arrays into one single array
    : for(int z = 0; z < array1.length; z++){
    : array[z] = array1[z];
    : }
    : for(int x = array1.length, i = 0; x < array.length; x++, i++){
    : array[x] = array2[i];
    : }
    :
    : for(int i = 0; i < array.length; i++)
    : System.out.print(array[i] + " ");
    : System.out.println();
    :
    : /*
    : Bubble Sort:
    : This is necasary in order for the printing loops to work properly.
    : Note that the inner for loop could test to array.length - 1, but
    : that would cause the last number to not be sorted if it were to equal a number
    : that was already in the array.
    : */
    :
    : for(int index = 0; index < array.length; index++){
    : min = index;
    : for(int test = index+1; test < array.length; test++)
    : if(array[test] < array[min])
    : min = test;
    : temp = array[min];
    : array[min] = array[index];
    : array[index] = temp;
    : }
    :
    : //test to see if sort works
    : //for(int z = 0; z < array.length; z++)
    : //System.out.println(array[z]);
    :
    : //Count number of cases of each element
    : for(int i = 0; i < array.length; i++){
    : cases = 0;
    : current = array[i];
    : for(counter = 0; counter < array.length; counter++){
    : if(array[counter] == current){
    : cases++;
    : }
    : }
    : //This if is used to ensure that the same number won't print twice
    : if(i != array.length - 1 && array[i] == array[i+1])
    : continue;
    : else
    : System.out.println(current + " times occured: " + cases);
    : }
    : }
    : }
    :
    :
    : Any tips as to what I might to differently would help out alot. Thanks for your time.
    : -BigQ
    :

  • Thanks for your help Muruga.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories