Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java program to group consecutive number from given numbers in a list<list>

Suppose you are given unique numbers like

11,2,7,6,17,13,8,9,3,5,12

The result will be group of numbers list containing sub list i.e.,

[2,3]-[5,6,7,8,9]-[11,12,13]-[17]

I took this approach to solve this below:

int[] a = { 11, 2, 7, 6, 13,17, 8, 9, 3, 5, 12 };

Arrays.sort(a);
List<List<Integer>> ListMain = new ArrayList<>();
List<Integer> temp = new ArrayList<>();

for (int i = 0; i < a.length; i++) {
    if (a[i + 1] == a[i] + 1) {
        temp.add(a[i + 1]);
    } else {
        ListMain.add(temp);
        temp.clear();
    }
}
like image 618
user0808 Avatar asked Feb 11 '23 20:02

user0808


2 Answers

Your overall logic is mostly correct. You have a few problems in your execution, though.

  1. You get an array index out of bounds exception because you call a[i+1] when i = a.length. Change the loop condition to a.length - 1.
  2. You need to use a new inner ArrayList each time, otherwise each array will get wiped out. Change temp.clear(); to temp = new ArrayList<>();.
  3. You need to initialize the new list to the first element, not the empty list. Add temp.add(a[0]); at the beginning and temp.add(a[i+1]) when you've detected you need a new sublist.
  4. You need to add the final list at the end, because your condition won't get called at the end of the loop.

Here's the modified program:

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class SubList {
    public static void main(String... args) {
        int[] a = { 11, 2, 7, 6, 13,17, 8, 9, 3, 5, 12 };

        Arrays.sort(a);
        List<List<Integer>> ListMain = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        temp.add(a[0]);

        for (int i = 0; i < a.length - 1; i++) {
            if (a[i + 1] == a[i] + 1) {
                temp.add(a[i + 1]);
            } else {
                ListMain.add(temp);
                temp = new ArrayList<>();
                temp.add(a[i+1]);
            }
        }

        ListMain.add(temp);

        System.out.println(ListMain);
    }
}

Output:

[[2, 3], [5, 6, 7, 8, 9], [11, 12, 13], [17]]
like image 198
durron597 Avatar answered Feb 14 '23 10:02

durron597


Thanks Garis M Suero for your suggestion after which i got answer

    int[] a = { 11, 2, 7, 6, 13,17, 8, 9, 3, 5, 12 };

    Arrays.sort(a);

    List<List<Integer>> listMain = new ArrayList<List<Integer>>();
    List<Integer> temp = new ArrayList<>();

    for (int i = 0; i < a.length; i++) {
        if ((i + 1<a.length)&&( a[i] + 1==a[i + 1])) {
            temp.add(a[i]);
        } else {
            temp.add(a[i]);
            listMain.add(temp);
            temp  = new ArrayList<>();
        }

    }

    for (List<Integer> lstI : listMain) {
            for (Integer i : lstI) {
                System.out.println(i);
            }
        System.out.println("================");
     }
like image 30
user0808 Avatar answered Feb 14 '23 10:02

user0808