Results 1 to 20 of 20
Thread: first post, array problem
- 09-29-2010, 11:33 PM #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.!
- 09-30-2010, 03:56 AM #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:
So after this processing, the map will contain (in your example).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)); } }
1: 1
2: 1
3: 4
4: 1
- 10-02-2010, 08:34 PM #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.
-
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 #5
Member
- Join Date
- Jan 2008
- Posts
- 9
- Rep Power
- 0
I quickly came up with a solution. PM me if you have any questions.
Output:Java Code:public class Main { public static void main (String [] args) { int[] array = {1,2,3,4,3,3,3,}; /*** solution deleted ***/ } }
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.
- 10-03-2010, 02:27 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.
-
- 10-03-2010, 03:20 AM #8
Member
- Join Date
- Jan 2008
- 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.
- 10-03-2010, 06:32 AM #9
You might also want to delete
A forum is a place to share problems and their possible solutions. Discussions in private messages defeats that goal.PM me if you have any questions.
db
- 10-03-2010, 09:00 AM #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!
- 10-03-2010, 05:15 PM #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?
- 10-03-2010, 05:37 PM #12
You'll have to post your code so we can see what your problem is.
- 10-03-2010, 05:43 PM #13
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
it works good but it printsint[] 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]]);
}
i would like1 1
4 1
4 2
5 1
6 1
4 3
3 1
please give me a hint how to do it, thank you1 1
4 3
5 1
6 1
3 1
- 10-03-2010, 05:50 PM #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.
- 10-03-2010, 05:53 PM #15
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
yeah, it works for frequency, but items array still prints all the values
- 10-03-2010, 05:58 PM #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.
- 10-04-2010, 05:31 AM #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..
- 10-04-2010, 08:29 AM #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.. ;)
- 10-04-2010, 10:00 AM #19
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
I immediately see a problem with the solutions posted already, you use your frequency array like this:
but initialize the array like this:Java Code:frequency[number] = timesCounted
What happens when you use this for your input?Java Code:originalArray[] = {something something}; frequency[] = new array[originalArray.length];
Java Code:originalArray[] = {100,101};Ever seen a dog chase its tail? Now that's an infinite loop.
- 10-05-2010, 02:27 AM #20
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
DOnt know if 1st post if did, I am VERY sorry for duplicate post. I have error messg
By afisher300 in forum New To JavaReplies: 3Last Post: 05-04-2009, 03:15 AM -
Problem With POST command to control the network camera
By sri_reddy523 in forum NetworkingReplies: 3Last Post: 03-16-2009, 06:53 AM -
Storing Array from HTTP Post
By kskinner in forum New To JavaReplies: 1Last Post: 09-08-2008, 06:00 AM -
Problem creating a Post script file
By krishnan.1000 in forum New To JavaReplies: 0Last Post: 02-14-2008, 07:15 PM -
array problem
By wats in forum New To JavaReplies: 1Last Post: 12-12-2007, 07:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks