-1

This function is supposed to get the indices of a matched element in a 2D array (OriginalArray). But because of duplicates, it keeps on scanning the values and ultimately replacing the indices assigned to variables a,b with the last element found. How can I get to store the index of value matched first and then stop searching?.

This might be simple for you,

OriginalArray

15 15 14 15 12 06 12
14 13 10 12 15 17 15
15 15 09 11 08 15 15
16 17 08 16 15 07 05
19 18 19 18 17 15 14

Code:

int row=5;
int col=7;
int [][] OriginalArray = new int [row][col];

int a=0,b=0;
for ( int i = 0; i < row; ++i ) {
    for ( int j = 0; j < col; ++j ){
        if( OriginalArray[i][j] == 8  ) {
            // Found the correct i,j - print them or return them or whatever
        System.out.println("{"+i+","+j+"}");
    System.out.println();
            a=i;
            b=j;
            break;
        } 
    }
}
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
Shirish
  • 7
  • 2

4 Answers4

1

You are doing it right, but the break statement only exits the inner loop. You have to break from the outer loop to maintain the indexes.

For the lazy:

int row=5;
int col=7;
int [][] OriginalArray = new int [row][col];

int a=0,b=0;
outerLoop:
for ( int i = 0; i < row; ++i ) {
    for ( int j = 0; j < col; ++j ){
        if( OriginalArray[i][j] == 8  ) {
            // Found the correct i,j - print them or return them or whatever
            System.out.println("{"+i+","+j+"}");
            System.out.println();
            a=i;
            b=j;
            break outerLoop;
        } 
    }
}
Community
  • 1
  • 1
Narmer
  • 1,414
  • 7
  • 16
1

You can use return instead of break.

for ( int i = 0; i < row; ++i ) {
    for ( int j = 0; j < col; ++j ){
        if( OriginalArray[i][j] == 8  ) {
            System.out.println("{"+i+","+j+"}");
            System.out.println();
            a=i;
            b=j;
            return;
        } 
    }
}
Baby
  • 5,062
  • 3
  • 30
  • 52
  • 1
    This works if you put the loops inside a method, which I also consider the best way to handle this situation – Narmer Jul 01 '14 at 09:27
0

Just use a Set first time you insert the Integer it returns true, when you insert that the Set already contains it returns false, for the request of the first occurence (just as Narmer suggested):

    int row=5;
    int col=7;
    int [][] OriginalArray = new int [row][col];

    int a=0,b=0;
    Set<Integer> duplicates = new HashSet<Integer>();
    outer: for ( int i = 0; i < row; ++i ) {
        for ( int j = 0; j < col; ++j ){
            if (!duplicates.add(OriginalArray[i][j])) {
                // Found the correct i,j - print them or return them or whatever
                System.out.println("{"+i+","+j+"}");
                        break outer;
            }
        }
    }

If you need to save the values for posterior treatment just replace for a HashMap, like below:

    int row=5;
    int col=7;
    int [][] OriginalArray = new int [row][col];

    Map<Integer, List<Pair<Integer, Integer>>> duplicates = new HashMap<Integer, List<Pair<Integer, Integer>>>();
    for ( int i = 0; i < row; ++i ) {
        for ( int j = 0; j < col; ++j ){
            if (duplicates.containsKey(OriginalArray[i][j])) {
                duplicates.get(OriginalArray[i][j]).add(new Pair(i,j));
            } else {
                duplicates.put(OriginalArray[i][j], new ArrayList<Pair<Integer, Integer>>());
            }
        }
    }

You'll need to create the Pair class it's a simple class with two attributes.

Best regards

Deceiver
  • 324
  • 1
  • 2
  • 12
  • `Map>>` Woah! That's quite a monster for a simple task... I trust you it works because I rolled my eyes at the thought of analyzing it. That should be a ringing bell... – Narmer Jul 01 '14 at 09:38
  • Lol if you want to keep all the duplicates it's one way to go :) – Deceiver Jul 01 '14 at 09:41
  • If you have to create the `Pair` class, why don't let it handle the list? Then it simplifies in `Map` and make the call `duplicates.get(OriginalArray[i][j]).add(i,j);`. In `PairList` you add the `new Pair(i,j)` which is an inner private class. Again, this is all for readability. – Narmer Jul 01 '14 at 09:49
  • 1
    Didn't occur to me when doing this but you're right it simplifies :) and it's indeed more readable. – Deceiver Jul 01 '14 at 10:13
0

Keep a flag which can be used to exit outer loop when we meet a specific condition

int row=5;
int col=7;
int [][] OriginalArray = new int [row][col];

int a=0,b=0;
boolean found = false;
for ( int i = 0; i < row; ++i ) 
{
  for ( int j = 0; j < col; ++j )
  {
      if( OriginalArray[i][j] == 8  ) 
      {
          // Found the correct i,j - print them or return them or whatever
          System.out.println("{"+i+","+j+"}");
          System.out.println();
          a=i;
          b=j;
          found = true;
          break;
      }
   }
   if(found)
   {
     break;
   }
}
vinayknl
  • 1,242
  • 8
  • 18