【问题标题】:Implementation of merge sort to sort array and display any duplicate values from array [duplicate]实现合并排序以对数组进行排序并显示数组中的任何重复值[重复]
【发布时间】:2018-04-01 18:00:59
【问题描述】:

我制作了以下程序并实现了合并排序,它对用户填充的数组进行排序,然后从数组中选择任何重复的值。但是,我在主要方法中有一个小问题。示例:我输入数字 4 3 次,它会显示“duplicate: 4”两次;我的目标是让它只显示一次,与有多少个 4 重复无关。谢谢大家。

 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]);

        }
    }
}

}

【问题讨论】:

  • 我无法避免插入重复项,因为这是程序的范围,以显示数组中重复多次的任何数字。还是谢谢你!
  • 所以使用hasmap,其中数字是键,计数是值。像 (4, 2) 数字 4 重复两次。
  • 对hasmaps或集合不太熟悉,这就是为什么我想在这种情况下坚持使用数组

标签: java arrays duplicates


【解决方案1】:

我会试着走这条路:

  • 如果数组的长度为 1,则没有重复
  • 如果数组的长度 > 1,则遍历元素
    • 第一个元素不能重复
    • 如果下一个元素(假设未到达数组末尾)与当前元素具有相同的值,我发现了一个重复项。将副本添加到类似 Set 的内容中。
    • 继续下一次迭代。
  • 对数组的迭代完成后,打印 Set-like 结构的所有元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 2017-04-28
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多