1

I know I have to do it with a while or do while loop, but I can't get it working. I also tried with a for loop, but it always gives me an error because I don't know the exact length of the vectors because they are random.

int a = (int)(Math.random() * 3 + 1);
int b = (int)(Math.random() * 3 + 1);
int c = a + b;
int[] arrA = new int[a];
int[] arrB = new int[b];
int[] arrC = new int[c];
for (int i = 0; i < a; i ++) {
    arrA[i] = (int)(Math.random() * 10 + 1);
    for (int j = 0; j < b; j ++) {
        arrB[j] = (int)(Math.random() * 10 + 1);
    }
}

Arrays.sort(arrA);
Arrays.sort(arrB);
System.out.println(Arrays.toString(arrA));
System.out.println(Arrays.toString(arrB));
System.out.println(Arrays.toString(arrC));
2
  • Nothing is being inserted in arrC Commented Jul 25, 2019 at 7:21
  • i know, cuz i dont know how, i tried with a for would compare A[x] with B[x] and if someone is lower it put it in arrC[x] but it doenst work Commented Jul 25, 2019 at 7:22

1 Answer 1

1

Take values from arrays arrA and arrB, and insert to arrC

int index = arrA.length;
for (int i = 0; i < arrA.length; i++) {
    arrC[i] = arrA[i];
}
for (int i = 0; i < arrB.length; i++) {
    arrC[i + index] = arrB[i];    
}

Sort arrC

Arrays.sort(arrC);

Reverse the order and store in arrD

for(int l = 0; l < arrC.length; l++) {
    arrD[l] = arrC[arrC.length - (l+1)];
}

Remove duplicate (simplified)

Set<Integer> remove=new LinkedHashSet<Integer>();
for(int i = 0;i < arrD.length;i++){
    remove.add(arrD[i]);
}

Remove duplicate (usual)

int index2 = 0;
for (int i = 0; i < arrD.length; i++) {
    for (int k = 0; k < arrD.length; k++) {
        if (arrD[i] != arrD[k]) {
            arrE[index2] = arrD[i];
            index2++;
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

How about int[] arrC = ArrayUtils.addAll(arrA, arrB); instead the first block?
@Blogger This doesn't work on Array, only on ArrayList.
is there a way to remove the duplicate without that method? i didnt saw that in my class
@kuroky You can compare each value with every other values in the array, but that will take a lot of time (writing code and performance).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.