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.

0

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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

Comments

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

Comments

-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

5 Comments

Hi @Manuli great to hear this. If this or any answer has solved your question please consider accepting it 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.
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
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
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, here is the question. stackoverflow.com/questions/32885907/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.