1

My java project is having a little issue here, I simplified it to the following codes. My codes print out:

Index 0 and 1 are pointing to same value. Index 1 and 0 are pointing to same value. Index 3 and 4 are pointing to same value. Index 4 and 3 are pointing to same value.

But what I want is:

Index 0 and 1 are pointing to same value. Index 3 and 4 are pointing to same value.

Because 0 and 1, 1 and 0 are really same pairs, any suggestion?

int[] arr={4,4,5,6,6,};
    
    for(int n=0; n<arr.length; n++)
    {
        for(int m=0; m<arr.length; m++)
        {
            if(arr[n]==arr[m] && n!=m)
            {
                System.out.println("Index "+n+" and "+m+" are pointing to same value.");
            }
        }
    }

5 Answers5

3

If you want to filter out the duplicate ones, change for(int m=0; m<arr.length; m++) to for(int m=n+1; m<arr.length; m++)

Max Peng
  • 2,879
  • 1
  • 26
  • 43
1

Try this.

int[] arr = {4,4,5,6,6,};

for(int n=0; n<arr.length; n++)
{
    for(int m=0; m<arr.length; m++)
    {
        if(arr[n]==arr[m] && n!=m && m > n || n == 0 && m == 0)
        {
            System.out.println("Index "+n+" and "+m+" are pointing to same value.");
        }
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • thanks for sharing, one more question, I tried m>n, and it prints correct results, so do you think || n == 0 && m == 0 could be redundant> – ArtStyle.Qwerty Nov 19 '21 at 05:32
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 19 '21 at 07:14
1
    int[] arr={4,4,5,6,6,};

    for (int n = 0; n < arr.length; n++) {
        for (int m = 0; m < arr.length; m++) {
            if (arr[n] == arr[m] && n != m) {
                if (m > 0 && n + 1 < arr.length) {
                        System.out.println("Index " + n + " and " + m + " are pointing to same value.");
                }
            }
        }
    }
Alexandros Kourtis
  • 539
  • 2
  • 6
  • 20
1

You can use hash map to get faster.

int[] arr = {4, 4, 5, 6, 6,};
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < arr.length; i++) {
        int num = arr[i];
        if (map.containsKey(num)) {
            System.out.println("Index " + map.get(num) + " and " + i + " are pointing to same value.");
            map.remove(num);
        } else
            map.put(arr[i], i);
    }
KuizhiWang
  • 61
  • 2
0

You may try to learn some problem solving skills when solving problem like decomposition recursion think back and so on
https://www.youtube.com/watch?v=S47aSEqm_0I
The lecture can teach you a lot.

int[] arr={4,4,5,6,6,};

        for(int n=0; n<arr.length; n++)
        {
            for(int m=n; m<arr.length; m++)
            {
                if(arr[n]==arr[m] && n!=m)
                {
                    System.out.println("Index "+n+" and "+m+" are pointing to same value.");
                }
            }
        }
KMING
  • 11
  • 5