# first post, array problem

• 09-29-2010, 11:33 PM
jaremah
first post, array problem
Hello, this is my first post on this forum. I have a question. I have an array
int[] = {1,2,3,4,3,3,3,}
I would like to print all the items (which is easy) and how many times each item appeared. I was struggling with that for few days already and I can't figure it out. Please any hints! thank you for all your help.!
• 09-30-2010, 03:56 AM
travishein
for printing how many times something appears, how about creating a Map where the key is the item and the value is an Integer variable that contains the number of times this has been seen so far. So the logic might be something like:
Code:

```Map<Integer,Integer> intMap = new HashMap<Integer,Integer>(); for (int value : intArr) {   String key = new Integer(value);   if (intMap.containsKey(key)) {     Integer existing = intMap.get(key);     intMap.put(key, new Integer(existing.intValue() + 1));   }   else {     intMap.put(key, new Integer(1));   } }```
So after this processing, the map will contain (in your example).
1: 1
2: 1
3: 4
4: 1
• 10-02-2010, 08:34 PM
jaremah
thank you Sir! However, I am not allowed to use any kinds of maps, or hash. Just arrays, and loops.
• 10-02-2010, 09:00 PM
Fubarable
Then if the numbers contained in the array are limited in some way with an upper and lower bound (perhaps they're all 10 or less, and all greater than 0), then use an array going from 0 to the upper bound and increment the ith element of the frequency array each time you encounter i in the original array.
• 10-03-2010, 01:36 AM
jac0117
I quickly came up with a solution. PM me if you have any questions.

Code:

``` public class Main { public static void main (String [] args) {     int[] array = {1,2,3,4,3,3,3,};     /*** solution deleted ***/ } }```
Output:
Code:

```All elements: 1 2 3 4 3 3 3 Occurrences: 1 found 1 times 2 found 1 times 3 found 4 times 4 found 1 times```
• 10-03-2010, 02:27 AM
Norm
@jac0117
Are you trying to help the OP learn how to program? Giving full solutions for their problems short circuits a lot of the learning process. If the OP doesn't do it himself and make the mistakes that everyone makes, he will not learn how to program. Reading other peoples' code is a passive process. Writing your code is an active process. You learn LOTS more doing it yourself.
• 10-03-2010, 03:17 AM
Fubarable
Quote:

Originally Posted by Norm
@jac0117
Are you trying to help the OP learn how to program? Giving full solutions for their problems short circuits a lot of the learning process. If the OP doesn't do it himself and make the mistakes that everyone makes, he will not learn how to program. Reading other peoples' code is a passive process. Writing your code is an active process. You learn LOTS more doing it yourself.

I strongly second this recommendation!
• 10-03-2010, 03:20 AM
jac0117
Sorry guys. You are right. If OP doesn't try then I guess he won't really learn. I deleted the solution.
• 10-03-2010, 06:32 AM
DarrylBurke
You might also want to delete
Quote:

PM me if you have any questions.
A forum is a place to share problems and their possible solutions. Discussions in private messages defeats that goal.

db
• 10-03-2010, 09:00 AM
John_shok
I am new to java too.. its interesting question! Any hints would be helpful to him and me!
• 10-03-2010, 05:15 PM
jaremah
okay, thanks for you help, I got it to count nicely, but now I don't want to print the whole array because its printing 3 for example 3 times. I want it to print 3 one time and right next to it how many times it appeared in array.

any hints?
• 10-03-2010, 05:37 PM
Norm
You'll have to post your code so we can see what your problem is.
• 10-03-2010, 05:43 PM
jaremah
Quote:

int[] items = {1,4,4,5,6,4,3};
int[] frequency = new int[items.length];
for (int i = 0; i < items.length;i++){
sub = items[i];
frequency[sub] = frequency[sub] + 1;
System.out.print(items[i]);
System.out.println( " " + frequency[items[i]]);
}
it works good but it prints

Quote:

1 1
4 1
4 2
5 1
6 1
4 3
3 1
i would like

Quote:

1 1
4 3
5 1
6 1
3 1
please give me a hint how to do it, thank you
• 10-03-2010, 05:50 PM
Norm
Don't count and print at the same time in the same loop.
You need to count first in one loop and then print out the results when done counting.
• 10-03-2010, 05:53 PM
jaremah
yeah, it works for frequency, but items array still prints all the values
• 10-03-2010, 05:58 PM
Norm
Why are you printing out the full input array? Obviously that is not what you want.
Find a way to get the number that corresponds with the frequency values.
• 10-04-2010, 05:31 AM
John_shok
Hi jeremah.. I was looking at your code.. it is still not good..because if you look at ur element 4 .you have different frequency number... I am trying to solve your problem as well .. so far I got to this..

int[] a={1,4,4,5,6,4,3};

for(int i=0;i<a.length;i++){
int sum=0;
int z=a[i];
System.out.print(z+" | ");
for(int j=0;j<a.length;j++){
if(a[j]==z){
sum+=1;
}

}
System.out.println(sum);
}

output :
1 | 1
4 | 3
4 | 3
5 | 1
6 | 1
4 | 3
3 | 1

now all we need to find out is how to take out common numbers..
• 10-04-2010, 08:29 AM
John_shok
oh my gosh! i CAN'T believe it .. i actually solved this problem:
int[] a={1,1,3,3,4,4,5,6,4,3};

int x=a.length;// find out the size of 1st array
int[] b=new int[x];// make 2nd array from the size u got from 1st array
for(int y=0;y<a.length;y++){//loop the 1st array
int w=a[y];//get the value of arrays and assign it to a variable
b[w]=w;// say your value was 1, b[1]=1, b[3]=3 we dont have 2 so b[2]=0
}

for(int m=0;m<b.length;m++){//this is the loop for 2nd array
if(b[m]!=0){// this is sayin the run only arrays that dont have its value 0
int sum=0;
System.out.print(b[m]+" | ");//prints out the number llike 1,3,4 without duplication
for(int j=0;j<a.length;j++){//comparing the values from 2nd array with 1st array
if(a[j]==b[m]){//if it finds adds to sum
sum+=1;
}

}
System.out.println(sum);
}else{
}
}

i am practicing java too.. i hope this helps u.. ;)
• 10-04-2010, 10:00 AM
m00nchile
I immediately see a problem with the solutions posted already, you use your frequency array like this:
Code:

`frequency[number] = timesCounted`
but initialize the array like this:
Code:

```originalArray[] = {something something}; frequency[] = new array[originalArray.length];```
What happens when you use this for your input?
Code:

`originalArray[] = {100,101};`
• 10-05-2010, 02:27 AM
John_shok
Thanks for letting me know, you are right about that loops thingy. I should have created 3rd array that store all the value from second array, and use that 3rd arrays for looping..

and lol doggy thing was hilarious!!