Results 1 to 20 of 20
  1. #1
    jaremah is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Smile 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. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    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. #3
    jaremah is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    thank you Sir! However, I am not allowed to use any kinds of maps, or hash. Just arrays, and loops.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #5
    jac0117 is offline Member
    Join Date
    Jan 2008
    Posts
    9
    Rep Power
    0

    Default

    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 04:19 AM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    @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. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Norm View Post
    @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. #8
    jac0117 is offline Member
    Join Date
    Jan 2008
    Posts
    9
    Rep Power
    0

    Default

    Sorry guys. You are right. If OP doesn't try then I guess he won't really learn. I deleted the solution.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    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. #10
    John_shok is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    I am new to java too.. its interesting question! Any hints would be helpful to him and me!

  11. #11
    jaremah is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    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. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

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

  13. #13
    jaremah is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    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. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    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. #15
    jaremah is offline Member
    Join Date
    Sep 2010
    Posts
    5
    Rep Power
    0

    Default

    yeah, it works for frequency, but items array still prints all the values

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

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

    Default

    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. #18
    John_shok is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    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. #19
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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};
    Ever seen a dog chase its tail? Now that's an infinite loop.

  20. #20
    John_shok is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

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

Similar Threads

  1. Replies: 3
    Last Post: 05-04-2009, 04:15 AM
  2. Problem With POST command to control the network camera
    By sri_reddy523 in forum Networking
    Replies: 3
    Last Post: 03-16-2009, 07:53 AM
  3. Storing Array from HTTP Post
    By kskinner in forum New To Java
    Replies: 1
    Last Post: 09-08-2008, 07:00 AM
  4. Problem creating a Post script file
    By krishnan.1000 in forum New To Java
    Replies: 0
    Last Post: 02-14-2008, 08:15 PM
  5. array problem
    By wats in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 08:08 AM

Posting Permissions

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