1

I need to get the unique values of the first array index and this is how I tried.

public class Array {

    public static void main(String[] args) {

        int[][] array = { 
                  {100, 12 , 0, 3},
                  {100, 177, 0, 3},
                  {100, 233, 0, 3}, 
                  {100, 144242, 0, 3},
                  {100, 14, 0, 4},  
                  {100, 12234, 0, 4},
                  {100, 134, 1, 4},
                  {2, 15, 0, 3},
                  {23, 1533, 0, 3},
                  {23, 1322, 1, 4}, 
                  {23, 13, 1, 4}, 
                  {23, 122, 1, 4},
                  {1321, 142, 1, 4},
                  {1321, 133,1, 4},
                  {3, 16, 0, 5},
                  {55, 1003, 0,3},
                  {553, 1002, 2, 6},
                  {31, 162, 0, 5},
                  {7, 1626, 0, 5},
                  {7, 2336, 0,5}           
                 };




        boolean isUnique = true;

        for(int i=0; i<= array.length; i++)
        {
             for (int j = 0; j < 1; j++)
             {
                 if (array[j]==array[j])
                 {                       

                     int riid = array[i][j];    

                     Set<Integer> uniqueNumbers = new HashSet<Integer>(Arrays.asList(riid));

                     System.out.println(riid);
                 }               
             }
         }
    }
}

my output must be 100, 2, 23, 1321, 3, 55, 553, 31 and 7. but, it doesn't give me unique values. It prints 100 100 100 100 100 100 100 2 23 23 23 23 1321 1321 3 55 553 31 7 7

How can I get the unique values of this output. i thought Set<Integer> uniqueNumbers = new HashSet<Integer>(Arrays.asList(riid)); would help. but, it didn't.

clD
  • 2,523
  • 2
  • 22
  • 38

4 Answers4

2

You just need to write:

    Set<Integer> uniqueNumbers = new LinkedHashSet<Integer>();
    for (int i = 0; i < array.length; i++) {
        uniqueNumbers.add(array[i][0]);
    }
    System.out.println(uniqueNumbers);
Xavier Delamotte
  • 3,519
  • 19
  • 30
  • `LinkedHashSet` will preserve the insertion order afaik, so it's the only answer so far that uses a set and works as intended :p – Łukasz Oct 01 '15 at 08:33
  • @Łukasz actually if you read question carefully OP does not need the `Set`. Is just used because OP does not finded a solution, OP only needs to print... – Jordi Castilla Oct 01 '15 at 09:04
1

You have many problems in your code:

  • i <= array.length - Arrays are zero-based in Java
  • Your inner loop runs from 0 to 1, what exactly are you trying to achieve here?
  • You're overriding the set on each iteration, which is not exactly what you want

There are many possible solutions for your problem, one of them is flatten the 2D-array (with Java 8 it's pretty easy), and then convert it to a Set:

int[] myArr = Arrays.stream(array)
            .flatMapToInt(Arrays::stream)
            .toArray();

Set<Integer> mySet = new HashSet<>(Arrays.asList(myArr));
Maroun
  • 94,125
  • 30
  • 188
  • 241
0

That's simple create an ArrayList and check if it contains the first element of each array then don't add this element to the ArrayList and if it doesn't contain it add it to the ArrayList

 ArrayList<Integer> list = new ArrayList<>();
      for(int i = 0 ; i < array.length ;i++ ){
              if(!list.contains(array[i][0])){
                  list.add(array[i][0]);
              }
      }

here is the working exapmle

karim mohsen
  • 2,164
  • 1
  • 15
  • 19
-1

This condition compares same value, so will be always true:

if (array[j]==array[j])
//        ↑         ↑

In my understanding you just need to check first position:

int lastRiid = 0;

for (int i = 0; i < array.length; i++) {
    if (array[i][0] != lastRiid) {
        lastRiid = array[i][0];
        System.out.println(lastRiid);
    }
}

OUTPUT:

100
2
23
1321
3
55
553
31
7
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Hi @Manuli great to hear this. If this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Jordi Castilla Oct 01 '15 at 09:03
  • Can you help me with print the list of 1st indexes which has the similar 0th index. ex - 100 - 12,177,233,144242, 14, 12234, 134 –  Oct 01 '15 at 10:44
  • tried. but it prints only first value. :( This is what I got. 100-12 2-15 23-1533 1321-142 3-16 55-1003 553-1002 31-162 7-1626 –  Oct 01 '15 at 10:46
  • i should know where are you stuck... and can't add so much code in comments clear, please, **post another question and paste here the link**, I will take a look... – Jordi Castilla Oct 01 '15 at 10:47
  • Jordi Castilla, here is the question. http://stackoverflow.com/questions/32885907/print-unique-values-from-array-and-print-the-list-of-1st-indexes-which-has-the-s –  Oct 01 '15 at 11:00