grouping data of array

Joined
Aug 1, 2011
Messages
1
Reaction score
0
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
 
Joined
Aug 3, 2011
Messages
1
Reaction score
0
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 :)
 
Joined
Oct 24, 2010
Messages
13
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top