简体   繁体   English

数组中的重复元素

[英]repeated element in Array

I have an array of N elements and contain 1 to (N-1) integers-a sequence of integers starting from 1 to the max number N-1-, meaning that there is only one number is repeated, and I want to write an algorithm that return this repeated element, I have found a solution but it only could work if the array is sorted, which is may not be the case. 我有一个由N个元素组成的数组,并且包含1到(N-1)个整数-从1到最大数字N-1-的整数序列,这意味着仅重复一个数字,我想写一个返回此重复元素的算法,我发现了一个解决方案,但它只能在数组排序后才能工作,情况可能并非如此。 ?

int i=0;
while(i<A[i])
{
i++
}
int rep = A[i];

I do not know why RC removed his comment but his idea was good. 我不知道为什么RC删除了他的评论,但是他的想法很好。

With the knowledge of N you easy can calculate that the sum of [1:N-1]. 有了N的知识,您可以轻松计算出[1:N-1]的总和。 then sum up all elementes in your array and subtract the above sum and you have your number. 然后将数组中的所有元素求和,然后减去上述总和,就得到数字。

This comes at the cost of O(n) and is not beatable. 这是以O(n)为代价的,是不可击败的。

However this only works with the preconditions you mentioned. 但是,这仅适用于您提到的前提条件。

A more generic approach would be to sort the array and then simply walk through it. 一种更通用的方法是对数组进行排序,然后简单地遍历它。 This would be O(n log(n)) and still better than your O(n²). 这将是O(n log(n)),但仍然比O(n²)好。

I you know the maximum number you may create a lookup table and init it with all zeros, walk through the array and check for one and mark the entries with one. 我知道您可以创建查找表并将其初始化为全零的最大数量,遍历数组并检查1,然后将条目标记为1。 The complexity is also just O(n) but at the expense of memory. 复杂度也仅为O(n),但以内存为代价。

if the value range is unknown a simiar approach can be used but instead of using a lookup table a hashset canbe used. 如果值范围未知,则可以使用类似的方法,但是可以使用哈希集代替哈希表。

Linear search will help you with complexity O(n): 线性搜索将帮助您提高复杂度O(n):

final int n = ...;
final int a[] = createInput(n); // Expect each a[i] < n && a[i] >= 0
final int b[] = new int[n];

for (int i = 0; i < n; i++)
    b[i]++;

for (int i = 0; i < n; i++)
   if (b[i] >= 2)
      return a[i];

throw new IllegalArgumentException("No duplicates found");

A possible solution is to sum all elements in the array and then to compute the sym of the integers up to N-1. 一种可能的解决方案是对数组中的所有元素求和,然后计算不超过N-1的整数的符号。 After that subtract the two values and voila - you found your number. 之后,减去两个值,瞧-您找到了您的电话号码。 This is the solution proposed by vlad_tepesch and it is good, but has a drawback - you may overflow the integer type. 这是vlad_tepesch提出的解决方案,虽然不错,但有一个缺点-您可能会溢出整数类型。 To avoid this you can use 64 bit integer. 为了避免这种情况,您可以使用64位整数。

However I want to propose a slight modification - compute the xor sum of the integers up to N-1(that is compute 1^2^3^...(N-1)) and compute the xor sum of your array(ie a 0 ^a 1 ^...a N-1 ). 但是我想提出一个小的修改-计算不超过N-1的整数的异或(即计算1 ^ 2 ^ 3 ^ ...(N-1))并计算数组的异或(即a 0 ^ a 1 ^ ... a N-1 )。 After that xor the two values and the result will be the repeated element. 之后,将这两个值进行异或运算,结果将是重复元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM