grouping data of array

Discussion in 'Java' started by neda, Aug 1, 2011.

  1. neda

    neda

    Joined:
    Aug 1, 2011
    Messages:
    1
    I have sorted array of integer . how can I define new arrays with similar data .
    indeed I want to grouping similar data of array .
    example:
    input : int num[] = {20,21,21,24,26,26,26,30};
    output : A={20}
    B={21,21}
    C={24}
    D={26,26,26}
    E={30}
    thanks
    neda, Aug 1, 2011
    #1
    1. Advertising

  2. neda

    nupur.kinger

    Joined:
    Aug 3, 2011
    Messages:
    1
    Hi Neda,

    Well, I think the following code of lines should work for you :

    int a = num[0];
    List<Integer[]> listOfArrays = new ArrayList<Integer[]>();
    int count = 0;
    for(int i = 0 ; i<num.length ; i++) {
    if(a == num) {
    count ++;
    } else {
    Integer[] arr = new Integer[count];
    for(int j = 0 ; j<arr.length ; j++) arr[j] = a;
    listOfArrays.add(arr);
    a = num;
    count = 1;
    }
    }
    Integer[] arr = new Integer[count];
    for(int j = 0 ; j<arr.length ; j++) arr[j] = a;
    listOfArrays.add(arr);

    The required output arrays are collected in a list - listOfArrays.

    Hope that helps :)
    nupur.kinger, Aug 4, 2011
    #2
    1. Advertising

  3. neda

    javabudy

    Joined:
    Oct 24, 2010
    Messages:
    13
    There is nothing available in API as per your need. as nupur suggested you need to write your own routine. you can also leverage no duplication feature of Set to achieve same thing

    Javin
    javabudy, Aug 6, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brad Foster

    Grouping array data

    Brad Foster, Oct 24, 2003, in forum: Java
    Replies:
    9
    Views:
    2,601
  2. pkilambi@gmail.com

    grouping array

    pkilambi@gmail.com, Sep 29, 2005, in forum: Python
    Replies:
    8
    Views:
    336
    Michael Spencer
    Sep 30, 2005
  3. Debbie Davis

    Grouping totalling maybe an array?

    Debbie Davis, Oct 19, 2004, in forum: ASP General
    Replies:
    6
    Views:
    143
    Debbie Davis
    Oct 20, 2004
  4. Sam Kong

    Grouping elements of an array?

    Sam Kong, Jan 8, 2007, in forum: Ruby
    Replies:
    6
    Views:
    123
    Sam Kong
    Jan 8, 2007
  5. Sam Kong
    Replies:
    5
    Views:
    324
    Peña, Botp
    Oct 22, 2007
Loading...

Share This Page