-2

I have this integer array called numList which has

[4, 4, 3, 3, 3, 2, 1, 1, 1, 1, -1, -12, -12, -12, -12]

I would like to create a multidimensional array which can store

enter image description here

Which left side represents the number and the right side determines the number of occurrences.

The attempt i tried... i got nowhere.

        // Declaring the new multi-dimensional array.
    int [] [] newArray = new int [6] [2];

    // Counter 3.
    int counter3 = 0;

    // Get first occurrence.    
    while (numList[counter3] < numList.length){



        for (int counter3:numList){
            newArray[] ([counter3]++);


        }
Kinny
  • 47
  • 6

3 Answers3

0
    private Map<Integer, Integer> segregateArray(List<Integer> list) {
    Map<Integer, Integer> result = new HashMap<>();
    for (Integer i : list) {
        if (result.containsKey(i)) {
            result.put(i, result.get(i) + 1);
        } else {
            result.put(i, 1);
        }
    }
    return result;
}

This should work. If you still need to return array use this:

private int[][] segregateArray(int[]list) {
    Map<Integer, Integer> resultHelper = new HashMap<>();
    for (int i : list) {
        if (resultHelper.containsKey(i)) {
            resultHelper.put(i, resultHelper.get(i) + 1);
        } else {
            resultHelper.put(i, 1);
        }
    }
    int[][] result = new int[resultHelper.size()][2];
    int arrayIterator=0;
    for(Integer key : resultHelper.keySet())
    {
        result[arrayIterator][0]=key;
        result[arrayIterator][1]=resultHelper.get(key);
        arrayIterator++;
    }
    return result;
}
graczun
  • 572
  • 1
  • 5
  • 16
  • do i need to import anything? – Kinny Sep 01 '15 at 14:00
  • import java.util.HashMap; import java.util.Map; – graczun Sep 01 '15 at 14:02
  • Thanks for the effort. i have been trying since you posted. As im a newbie, is this what i should use to call for your method? I did change the (int[] numList) and for (int i: numList) – Kinny Sep 01 '15 at 14:15
  • in main method(or wherever you want to use that) simply call this method int[] array = {4, 4, 3, 3, 3, 2, 1, 1, 1, 1, -1, -12, -12, -12, -12}; int [] [] newArray = segregateArray(array); – graczun Sep 01 '15 at 14:20
0

In the real life project you probably should avoid implementing a functionality like this yourself using a low level array mechanism (you added an extensive test suite, didn't you? :) and opt for one of available libraries.

In Java 8 this can be done nicely using closures similarly to what has been described here: Count int occurrences with Java8.

In Java 7 and earlier I would use one of the collection libraries such as Guava, which contains a Multiset collection delivering exactly what you're after.

Community
  • 1
  • 1
Norbert Radyk
  • 2,608
  • 20
  • 24
0

Assuming your numbers are in order as they are in your example numList, then you could do this:

int[] numList = { 4, 4, 3, 3, 3, 2, 1, 1, 1, 1, -1, -12, -12, -12, -12 };
int[][] newArray = new int[6][2];
int index = 0;

for (int i = 0; i < numList.length;) {
    int count = 0;
    for (int x = 0; x < numList.length; x++)
        if (numList[x] == numList[i]) count++;
    newArray[index][0] = numList[i];
    newArray[index][1] = count;
    index++;
    i += count;
}
for (int x = 0; x < newArray.length; x++) {
    for (int i = 0; i < newArray[0].length; i++)
        System.out.print(newArray[x][i] + " ");
    System.out.println();
}

This way, you don't have to deal with imports as in the other answers (and this is shorter), but this only works if you have ordered numbers. There are some good sorting algorithms out there, though.

Edit: I changed it so that it can take numbers in any order of any size.

int[] numList = { 6, 6, 5, 5, 4, 4, 3, 2, 1, 1, 1, 7, 6, 5, 7, 8, 65, 65, 7 };
int[][] newArray = new int[1][2];
int index = 0;
for (int i = 0; i < numList.length;) {
    try {
        int count = 0;
        boolean isUnique = true;
        for (int x = 0; x < i; x++)
            if (numList[x] == numList[i]) {
                isUnique = false;
                break;
            }
        if (isUnique) {
            for (int x = 0; x < numList.length; x++)
                if (numList[x] == numList[i]) count++;
            newArray[index][0] = numList[i];
            newArray[index][1] = count;
            index++;
        }
        i++;
    } catch (ArrayIndexOutOfBoundsException e) {
        int tmpArray[][] = newArray;
        newArray = new int[tmpArray.length + 1][tmpArray[0].length];
        for (int row = 0; row < tmpArray.length; row++)
            for (int col = 0; col < 2; col++)
                newArray[row][col] = tmpArray[row][col];
    }
}
for (int x = 0; x < newArray.length; x++) {
    for (int i = 0; i < newArray[0].length; i++)
        System.out.print(newArray[x][i] + " ");
    System.out.println();
}

So, at this point, it would probably be shorter to use the maps from the other answer. The only benefit of my second answer not worrying about imports.

17slim
  • 1,233
  • 1
  • 16
  • 21
  • I did a System.out.println(Arrays.toString(newArray)); [[I@63ce0e18, [I@6cff7cd8, [I@795d80cf, [I@69b3d448, [I@1d35f92f, [I@427a8ba4] was returned. – Kinny Sep 01 '15 at 14:21
  • That's because that's the identifier of each array in newArray. I updated the code to display it properly. – 17slim Sep 01 '15 at 14:30
  • thanks it worked now. awesome! im trying to analyse and learn something out of it. – Kinny Sep 01 '15 at 14:41