简体   繁体   English

使用C ++在数组中查找不同的元素

[英]find distinct elements in an array using c++

I have an array of integers. 我有一个整数数组。 I want to find all the distinct elements in the array using c++. 我想使用c ++查找数组中所有不同的元素。 solutin 1: BRUTE FORCE using nested loop, complexity of this solution is O(n^2) solutin 1:使用嵌套循环的BRUTE FORCE,此解决方案的复杂度为O(n ^ 2)

solution 2: SORTING this will take O(nLog n) 解决方案2:排序将花费O(nLog n)

Is there any other technique which can give better results than O(n Log n)? 是否有其他技术可以提供比O(n Log n)更好的结果? any other data structure or any different technique? 任何其他数据结构或任何其他技术?

使用std::unordered_set将为O(n)。

You can also try using std::nth_element : It partially sorts the range [first, n-th, last) such that all entries in the interval [first, n-th) are <= n-th, and all elements in the interval (n-th, last) are >= than n-th. 您也可以尝试使用std :: nth_element :它对范围[first,n-th,last)进行部分排序,以使间隔[first,n-th)中的所有条目均<= n-th,并且间隔(第n个,最后一个)大于第n个。 It has linear complexity (O(n)), and will find the n-th element in a sequence. 它具有线性复杂度(O(n)),并将在序列中找到第n个元素。 It's not suited to find a specific number, though, so maybe it's not exactly what you need. 但是,它不适合查找特定的数字,因此也许这并不是您真正需要的。 But it's worth to keep it in mind :-) 但值得记住的是:-)

If you know the maximum integer number, and it is reasonably small, you cann allocate a large vector and use that to count the frequency of each integer. 如果您知道最大整数数,并且它相当小,则可以分配一个大向量,然后使用它来计算每个整数的频率。 Then, iterate over the vector and find all with frequency one: 然后,遍历向量并找到频率为1的所有向量:

template<typename I>
auto findWithFrequency(int f, int max, I first, I last)
{
    std::vector<int> counts(max, 0);
    for(; first != last; ++first)
    {
        counts[*first] += 1;
    }

    std::vector<typename I::value_type> v;
    v.reserve( std::distance(first, last) );

    std::copy_if(counts.begin(), counts.end(),
                 std::back_inserter(v),
                 [f](auto x) {return x == f;});

    return v;
}

In the worst case, this needs two iterations over arrays of the size of the input array, so the complexity is O(n). 在最坏的情况下,这需要对输入数组大小的数组进行两次迭代,因此复杂度为O(n)。

This is essentially the idea behind Bucketsort or Radix. 这实质上是Bucketsort或Radix背后的想法。

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

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