user4605225
user4605225

Reputation:

print unique values from array

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.

Upvotes: 1

Views: 580

Answers (4)

Jordi Castilla
Jordi Castilla

Reputation: 26961

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

Upvotes: -1

karim mohsen
karim mohsen

Reputation: 2254

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

Upvotes: 0

Maroun
Maroun

Reputation: 95968

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));

Upvotes: 1

Xavier Delamotte
Xavier Delamotte

Reputation: 3599

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);

Upvotes: 2

Related Questions