0
I’m having trouble getting an algorithm that removes duplicate elements in an array (can only be integers), which uses the method Divide And Conquer. I am in need for a college job, and unfortunately I am not able to find/implement an algorithm in Java.
EDIT : I tried to create an algorithm without success, my initial idea was to do something that used the logic of Mergesorte, but instead of ordering the algorithm, whenever it found equal elements, it cut them from the original vector, but I got confused in the middle of the process. There is my implementation in JAVA, if anyone can show me what I did wrong, I will be grateful.
public static int[] merge(int[] leftArray, int[] rightArray) {
int leftArrayEnd = leftArray.length - 1;
int rightArrayEnd = rightArray.length - 1;
int leftArrayBegin = 0;
int rightArrayBegin = 0;
int numElements = leftArray.length + rightArray.length;
int[] resultArray = new int[numElements];
int resultArrayBegin = 0;
while (leftArrayBegin <= leftArrayEnd && rightArrayBegin <= rightArrayEnd) {
if (leftArray[leftArrayBegin] <= rightArray[rightArrayBegin]) {
resultArray[resultArrayBegin++] = leftArray[leftArrayBegin++];
} else if (leftArray[leftArrayBegin] > rightArray[rightArrayBegin]) {
resultArray[resultArrayBegin++] = rightArray[rightArrayBegin++];
} else {
//ITEMS IGUAIS
//INCREMENTA NA DIREITA
rightArrayBegin++;
}
}
while (leftArrayBegin <= leftArrayEnd) {
resultArray[resultArrayBegin++] = leftArray[leftArrayBegin++];
}
while (rightArrayBegin <= rightArrayEnd) {
resultArray[resultArrayBegin++] = rightArray[rightArrayBegin++];
}
return resultArray;
EDIT2: I found a possible solution to my problem, but it is in Python. It would be possible to convert it to Java?
def merge(left, right):
result = []
while left and right:
small, big = min(left, right), max(left, right)
result.append(small[0])
left, right = ((left[1:], right[1:])
if small[0] == big[0]
else (small[1:], big))
def remove_duplicates(candidates):
if not candidates or len(candidates) == 1:
return candidates
return merge(remove_duplicates(candidates[1::2]),
remove_duplicates(candidates[::2]))
Got nothing? Not even starting the program? So, considering it’s a college job, it seems to me you’ll need to revise the basics. Start by searching how you define yourself and work with a array and research what the technique is divides and Conquer. After that you will demonstrably be able, if you don’t do the exercise, to do a good part of it.
– Woss
I managed to make an algorithm, but it became unviable for big numbers, so I’m definitely on the other side
– JoubertJoe .Vieira Lellis
Then add to the question this algorithm, explain how you did it and what your problem is. With that we can reopen the question and help you.
– Woss
Right, I followed an example of stack overflow even trying to modify the merge function of Mergesort.
– JoubertJoe .Vieira Lellis
With a little experimentation I got a modification in the MERGE algorithm of merge and Sort, this solved my problem.
– JoubertJoe .Vieira Lellis