# Thread: first post, array problem

1. Member
Join Date
Sep 2010
Posts
5
Rep Power
0

## 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.!

2. 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:
Java 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

3. Member
Join Date
Sep 2010
Posts
5
Rep Power
0
thank you Sir! However, I am not allowed to use any kinds of maps, or hash. Just arrays, and loops.

4. 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.

5. Member
Join Date
Jan 2008
Location
http://www.helpwithprogramming.com/
Posts
9
Rep Power
0
I quickly came up with a solution. PM me if you have any questions.

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

/*** solution deleted ***/

}
}```
Output:
Java 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```
Last edited by jac0117; 10-03-2010 at 03:19 AM.

6. @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.

7. 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!

8. Member
Join Date
Jan 2008
Location
http://www.helpwithprogramming.com/
Posts
9
Rep Power
0
Sorry guys. You are right. If OP doesn't try then I guess he won't really learn. I deleted the solution.

9. You might also want to delete
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. Member
Join Date
Oct 2010
Posts
13
Rep Power
0
I am new to java too.. its interesting question! Any hints would be helpful to him and me!

11. Member
Join Date
Sep 2010
Posts
5
Rep Power
0
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?

12. You'll have to post your code so we can see what your problem is.

13. Member
Join Date
Sep 2010
Posts
5
Rep Power
0
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

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

1 1
4 3
5 1
6 1
3 1
please give me a hint how to do it, thank you

14. 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.

15. Member
Join Date
Sep 2010
Posts
5
Rep Power
0
yeah, it works for frequency, but items array still prints all the values

16. 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.

17. Member
Join Date
Oct 2010
Posts
13
Rep Power
0
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..

18. Member
Join Date
Oct 2010
Posts
13
Rep Power
0
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.. ;)

19. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
7
I immediately see a problem with the solutions posted already, you use your frequency array like this:
Java Code:
`frequency[number] = timesCounted`
but initialize the array like this:
Java Code:
```originalArray[] = {something something};
frequency[] = new array[originalArray.length];```
What happens when you use this for your input?
Java Code:
`originalArray[] = {100,101};`

20. Member
Join Date
Oct 2010
Posts
13
Rep Power
0
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!!

#### Posting Permissions

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