1

I am trying to loop through an array, however I am encountering this problem. As I loop through this array:

{1,2,3,4}

I am encountering this problem: During the start, I will get combinations of 1 and 4, however near the middle I will get a combination of 4 and 1. How can I make it so only unique relationships will be accepted? There cant be anything like {1,4} and {4,1}.

I am using Java, there have been some answers to this however they use libraries only available in other languages.

I can't wrap my head around it to come up with even an attempt at a solution unfortunately.

Here is the expected output after looping through the array:

{1, 2}
{1, 3}
{1, 4}
{2, 3}
{2, 4}
{3, 4}

But here is what actually happens when looping through the array:

{1, 1}
{1, 2}
{1, 3}
{1, 4}
{2, 1}
{2, 2}
{2, 3}
{2, 4}
{3, 1}
{3, 2}
{3, 3}
{3, 4}
{4, 1}
{4, 2}
{4, 3}
{4, 4}

So the two requirements is that the pair has to be a unique relationship (can't have 1,2 and 2,1) and they can't be the same either. Not being the same could easily be done by comparing the two numbers and seeing if they are equal, but I am having trouble with the first requirement.

Qasim
  • 1,686
  • 4
  • 27
  • 51

6 Answers6

7

After your update I assume you are looking for something like this

int[] arr={1,2,3,4};
for (int i=0; i<arr.length; i++)
    for (int j=i+1; j<arr.length; j++)
        System.out.println("{"+arr[i]+","+arr[j]+"}");

output:

{1,2}
{1,3}
{1,4}
{2,3}
{2,4}
{3,4}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
2

If you're trying to find all the possible unordered pairs from set one and two, then just do the following:

List<Pair> pairs = new ArrayList<Pair>();
for (int i : set1) {
    for (int j : set2) {
        pairs.add(new Pair(i, j));
    }
}
Zoltán
  • 21,321
  • 14
  • 93
  • 134
0

Usually you loop over two arrays like this:

for (int element1 : array1) {
   for (int element2 : array2) {
      .....
   } 
}
adranale
  • 2,835
  • 1
  • 21
  • 39
0
public static void main(String[] args) {
        int[] array1 = new int[] {1,2,3,4};
        int[] array2 = new int[] {5,6,7,8};
        String[] result = new String[array1.length * array2.length];
        int count = 0;
        for (int i : array1) {
            for (int j : array2) {
                result[count++] = "{" + i + ", " + j + "}";
            }
        }
        for (String str : result) {
            System.out.println(str);
        }
    }
M. Abbas
  • 6,409
  • 4
  • 33
  • 46
0

If you want to avoid repeated pairs, begin the inner loop at the current position in the outer loop.

for(int i = 0; i < array1.length; i++)
{
    for(int j = i + 1; j < array2.length; j++)
    {
        //do stuff
    }
}
Elliott
  • 1,336
  • 1
  • 13
  • 26
0

You want to treat a pair of objects as a comparable, and then create a set of pairs. Note that with this solution you will need to store your arrays as objects (e.g. Integer) rather than primitives (e.g. int). Check Convert an array of primitive longs into a List of Longs for more on that.

public class MyPair<A extends Comparable<A> > implements Comparable<MyPair<A>> {
  public A first; //You may want to make these private and implement getters, setters, etc
  public A second;
  public MyPair(A f, A s) { first = f; second = s; }
  public int compareTo(MyPair<A> o) {
     int cmp = first.compareTo(o.first);
     if(cmp == 0) cmp = second.compareTo(o.second);
     return cmp;
  }
  public String toString() {
    return "{"+first+","+second+"}";
  }
}

public static void main(String[] args) {
  Integer[] x = new Integer[]{1,2,3,4};
  Integer[] y = new Integer[]{5,6,7,8};
  Set<MyPair<Integer> > mypairs = new HashSet<MyPair<Integer> >();
  for(Integer f : x) 
  {
    for(Integer s : y) {
      MyPair<Integer> tmp = new MyPair<Integer>(f,s);
      mypairs.add(tmp);
    }
  }
  for(MyPair<Integer> pair : mypairs) {
    println(pair);
  }
}
Community
  • 1
  • 1
Arcymag
  • 1,037
  • 1
  • 8
  • 18