使用binarysearch从已排序的int []中删除重复项

时间:2013-06-11 09:21:06

标签: java arrays binary-search duplicate-removal

我有一个相当大的int [],它使用Arrays.sort()排序。我需要从数组中删除重复的元素。

这个问题源自sedgewick的算法书1.1.28

  

1.1.28删除重复项。修改BinarySearch中的测试客户端,以在排序后删除白名单中的任何重复键。

我尝试创建一个noDupes()方法,该方法接受一个int []并返回一个删除了重复项的int []

rank()方法来自sedgewick的代码。它执行二进制搜索

public static int[] noDupes(int[] a){
    Arrays.sort(a);
    int maxval= a[a.length-1];
    int[] nodupes = new int[maxval];
    int i=0;
    for(int j=0;j<a.length;j++){
        int rnk = rank(a[j],nodupes);
        System.out.println(a[j]+" rank="+rnk);
        if (rnk < 0){
            System.out.println(a[j]+" is not dupe");
            nodupes[i] = a[j];
            i++;
        }
    }

    return nodupes;
}
public static int rank(int key,int[] a){
    return rank(key,a,0,a.length-1);
}

public static int rank(int key,int[] a,int lo,int hi){
    if(lo > hi) return -1;
    int mid = lo+(hi-lo)/2;

    if(key < a[mid])return rank(key,a,0,mid-1);
    else if(key > a[mid])return rank(key,a,mid+1,hi);
    else return mid;
}

当我使用示例数组

运行它时
int[] a =new int[]{2,2,2,3,4,4,5,6};
int[] ret = noDupes(a);

我得到了一些意想不到的输出..只有在将2添加到nodupes数组后,现有元素的等级为-1 ..

2 rank=-1
2 is not dupe
2 rank=-1
2 is not dupe
2 rank=-1
2 is not dupe
3 rank=-1
3 is not dupe
4 rank=-1
4 is not dupe
4 rank=4
5 rank=-1
5 is not dupe
6 rank=-1
6 is not dupe
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at ...noDupes(BinSearch.java:85)
    at ...main(BinSearch.java:96)

我无法弄清楚我做错了什么..有人帮忙吗?

5 个答案:

答案 0 :(得分:3)

只需将所有数组值添加到HashSet中,它将自动删除重复项并为您提供唯一值,然后再将其转换为您需要的数组

答案 1 :(得分:2)

我会这样做

public static int[] noDupes(int[] a) {
    Arrays.sort(a);
    int noDupCount = 0;
    for (int i = 0; i < a.length; i++) {
        if (i == 0 || a[i] != a[i - 1]) {
            noDupCount++;
        }
    }
    int[] a2 = new int[noDupCount];
    for (int i = 0, j = 0; i < a.length; i++) {
        if (i == 0 || a[i] != a[i - 1]) {
            a2[j++] = a[i];
        }
    }
    return a2;
}

答案 2 :(得分:2)

如果您对数组进行了排序,并且如果要删除重复项,我认为您不需要使用二进制搜索。

对数组进行排序时,重复的元素将彼此相邻。

E.g。数组= {9,8,9,1,2,5,2,5,1} 排序后Array = {1,1,2,2,5,5,8,9,9}

您可以使用以下方式删除重复项(inplace)

int a[] = {sorted array}

for(int i=0,target=0;i<a.length-1;i++) {
  if(a[i]!=a[i+1]) {
     a[target++] = a[i];
  }
}
a[target++] = a[a.length-1];
for(int i=target;i<a.length;i++) {
a[i] = 0; // fill in the values which you don't want.
}

只会在一次通过中删除重复项

答案 3 :(得分:0)

这应该有所帮助:

int[] nodupes = new int[a.length];

nodupes数组越界了。

注意:我不确定您使用的逻辑是否最适合该问题。但这应该可以解决您的例外问题。

答案 4 :(得分:0)

此代码可以帮助您。

public Integer[] removeDuplicates(Integer[] input){
        Integer[] arrayWithoutDuplicates = null;
        Set<Integer> set = new LinkedHashSet<Integer>();
        for(int i : input){
            set.add(i);
        }
        arrayWithoutDuplicates = (Integer[]) set.toArray();
        return arrayWithoutDuplicates;
}