What is the fastest way to see if an array has two common elements?

new QOpenGLWidget

Suppose that we have two very long arrays, of, say, int to make the problem simpler.

What is the fastest way (or just a fast way, if it's not the fastest), in C++ to see if an array has more than one common elements in C++?

To clarify, this function should return this:

[2, 5, 4, 3] => false
[2, 8, 2, 5, 7, 3, 4] => true
[8, 8, 5] => true
[1, 2, 3, 4, 1, 7, 1, 1, 7, 1, 2, 2, 3, 4] => true
[8, '8', 3] => false
[9, 1, 12] => false

One strategy is to loop through the array and for each array element loop through the array again to check. However, this can be very costly and expensive (literally O(n^2)). Is there any better way?

JeJo

(Update Below) Insert the array elements to a std::unordered_set and if the insertion fails, it means you have duplicates.

Something like as follows:

#include <iostream>
#include <vector>
#include <unordered_set>

bool has_duplicates(const std::vector<int>& vec)
{
    std::unordered_set<int> set;
    for (int ele : vec)
        if (const auto [iter, inserted] = set.emplace(ele); !inserted)
            return true; // has duplicates!
    return false;
}

int main()
{
    std::vector<int> vec1{ 1, 2, 3 };
    std::cout << std::boolalpha << has_duplicates(vec1) << '\n'; // false

    std::vector<int> vec2{ 12, 3, 2, 3 };
    std::cout << std::boolalpha << has_duplicates(vec2) << '\n'; // true
}

Update: As discussed in the comments, this can or may not be the fastest solution. In OP's case, as explained in Marcus Müller's answer, anO(N·log(N)) method would be better, which we can achieve by having a sorted array check for dupes.

Here is a quick benchmark that I made for the two cases "UnorderedSetInsertion" and the "ArraySort". Following are the result for GCC 10.3, C++20, O3:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the fastest way to see if the values from a 2 dimensional array are present in 3 other two dimensional arrays

What is the fastest way to count elements in an array?

Fastest way of finding common elements between two list of lists in python

What is the fastest way to initialize an array in C with only two bytes for all elements?

What is the fastest way to get k smallest (or largest) elements of array in Java?

What's the fastest way to only rotate certain elements in an array?

What is the fastest way to set an arbitrary range of elements in a Java array to null?

What is the fastest way to select the smallest n elements from an array?

Fastest way to compare common items in two lists

What is fastest way to calculate factorial in common lisp?

What is the fastest way to change the order of elements in an ArrayList?

What is the fastest way to check a pandas dataframe for elements?

Fastest way to see which pixel has changed when using a listener

fastest way to check if all elements in an array are equal

Fastest way to replace all elements in a numpy array

Fastest way of iterating and accessing elements of numpy array?

easiest way to see if array has array? Javascript

What's the fastest way to threshold a numpy array?

What is the fastest way of appending an element to an array?

What is the fastest way to flatten an array of arrays in ocaml?

What is the fastest way to convert an object to an array in PHP?

What is the fastest way of computing powerset of an array in Python?

Fastest way to move unique array elements to the front and slice the array

Fastest way to calculate a heavy function between elements of two different lists

Fastest way to populate a matrix with a function on pairs of elements in two numpy vectors?

What is the fastest way to check if a class has a function defined?

What is the fastest way of finding if object has a value (multidimensional object)

What is the fastest way for adding the vector elements horizontally in odd order?

What is the fastest way to calculate the weighted sum of matrix elements?