-1

I have made the following program and implemented the merge sort where it sorts an array that the user fills and then picks any duplicate values from the array. However, I have a small problem in the main method. Example: I input the number 4 three times and it would display "duplicate: 4" two times; my goal here is to let it display only once, irrelevant of how many duplicates of 4 there are. Thanks guys.

 import java.util.*;

`public class Question6` {

   static void mergeSort(int arr1[], int o, int k, int x)  {
    int num1 = k - o + 1;
    int num2 = x - k;

    int temp1[] = new int [num1]; //creation of two temporary arrays to store in
    int temp2[] = new int [num2];


    for (int i=0; i<num1; ++i)   //for loops to copy the data to the temporary arrays
        temp1[i] = arr1[o + i];

    for (int j=0; j<num2; ++j)
        temp2[j] = arr1[k + o + j];





    int i = 0, j = 0; //starting position of temporary arrays


    int s = l; //starting position of the merged two temporary arrays
    while (i < num1 && j < num2)  {

        if (temp1[i] <= temp2[j])  {
            arr1[s] = temp1[i];
            i++;
        }
        else  {
            arr1[s] = temp2[j];
            j++;
        }
        s++;
    }

    //code to copy elements from temp1
    while (i < num1)
    {
        arr1[s] = temp1[i];
        i++;
        s++;
    }

   //code to copy elements from temp2
    while (j < num2)
    {
        arr1[s] = temp2[j];
        j++;
        s++;
    }
}

/
void forSorting(int arr2[], int o, int x) //main method that carries out merge sort
{
    if (o < x)
    {
        // Find the middle point
        int a = (o+x)/2;

        // Sort first and second halves
        forSorting(arr2, o, a);
        forSorting(arr2, a+1, x);

        // Merge the sorted halves
        mergeSort(arr2, o, a, x);
    }
}

public static void main(String[] args) {
    Question6 qs = new Question6();
    Scanner sc = new Scanner(System.in);
    int [] duplicate = new int[10];

    System.out.println("Please input the numbers to be checked for repetition.");

    for(int x = 0; x < 10; x++)
    {
        duplicate[x] = sc.nextInt(); //filling array
    }

    int length = duplicate.length;
    qs.forSorting(duplicate, 0, length-1); //calling method forSorting

    System.out.println(Arrays.toString(duplicate)); //displays array

    for (int count = 1; count < 10; count++)
    {
        if (duplicate[count] == duplicate[count - 1]) {
            //displays the duplicates
            System.out.println("Duplicate: " + duplicate[count]);

        }
    }
}

}

4
  • I cannot avoid inserting duplicates as that is the scope of the program, to display any number repeated more than once within the array. Thank you nonetheless! Commented Apr 1, 2018 at 18:10
  • So use a hasmap, where number is the key and the count is the value. like (4, 2) number 4 being repeated twice. Commented Apr 1, 2018 at 18:11
  • stackoverflow.com/questions/17630727/… Commented Apr 1, 2018 at 18:12
  • Not that familiar with hasmaps or sets in general, that's why I would like to stick with arrays for this case Commented Apr 1, 2018 at 18:16

1 Answer 1

0

I would try to go this way:

  • If the array has length 1, then there is no duplicate
  • If the array has length > 1, then iterate over the elements
    • The first element cannot be a duplicate
    • If the next element (assuming end of array not reached) has the same value as the current one, I have found a duplicate. Add the duplicate into something Set-like.
    • Continue with the next iteration.
  • After the iteration over the array is done, print all elements of the Set-like structure.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.