Find uncommon elements using hashing

Kashish Arora

I think this is a fairly common question but I didn't find any answer for this using hashing in C++.

I have two arrays, both of the same lengths, which contain some elements, for example:

A={5,3,5,4,2}
B={3,4,1,2,1}

Here, the uncommon elements are: {5,5,1,1}

I have tried this approach- iterating a while loop on both the arrays after sorting:

while(i<n && j<n) {
    if(a[i]<b[j])
            uncommon[k++]=a[i++];
    else if (a[i] > b[j])
            uncommon[k++]=b[j++];
    else {    
            i++;
            j++;
    }
}
while(i<n && a[i]!=b[j-1])
    uncommon[k++]=a[i++];
while(j < n && b[j]!=a[i-1])
    uncommon[k++]=b[j++];

and I am getting the correct answer with this. However, I want a better approach in terms of time complexity since sorting both arrays every time might be computationally expensive.

I tried to do hashing but couldn't figure it out entirely.

To insert elements from arr1[]:

set<int> uncommon; 
    for (int i=0;i<n1;i++) 
        uncommon.insert(arr1[i]); 

To compare arr2[] elements:

    for (int i = 0; i < n2; i++) 
        if (uncommon.find(arr2[i]) != uncommon.end()) 

Now, what I am unable to do is to send only those elements to the uncommon array[] which are uncommon to both of them.

Thank you!

smyatkin_max

First of all, std::set does not have anything to do with hashing. Sets and maps are ordered containers. Implementations may differ, but most likely it is a binary search tree. Whatever you do, you wont get faster that nlogn with them - the same complexity as sorting. If you're fine with nlogn and sorting, I'd strongly advice just using set_symmetric_difference algorithm https://en.cppreference.com/w/cpp/algorithm/set_symmetric_difference , it requires two sorted containers.

But if you insist on an implementation relying on hashing, you should use std::unordered_set or std::unordered_map. This way you can be faster than nlogn. You can get your answer in nm time, where n = a.size() and m = b.size(). You should create two unordered_set`s: hashed_a, hashed_b and in two loops check what elements from hashed_a are not in hashed_b, and what elements in hashed_b are not in hashed_a. Here a pseudocode:

create hashed_a and hashed_b
create set_result // for the result
for (a_v : hashed_a) 
  if (a_v not in hashed_b)
    set_result.insert(a_v)
for (b_v : hashed_b) 
  if (b_v not in hashed_a)
    set_result.insert(b_v)
return set_result // it holds the symmetric diference, which you need

UPDATE: as noted in the comments, my answer doesn't count for duplicates. The easiest way to modify it for duplicates would be to use unordered_map<int, int> with the keys for elements in the set and values for number of encounters.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to find uncommon elements from three arrays?

Compare two arrays and find the index of uncommon elements in SWIFT 3

How to find uncommon lines between two text files using shell script?

How to find the uncommon words from two files using only terminal commands in OSX?

Using a mix of uncommon characters in Python

Get uncommon elements from two list - KOTLIN

SQL Server find uncommon names in user registry

merge common elements of a list of dictionary and store uncommon elements in a new key

Hashing using a linkedlist

cluster using feature hashing

Find elements using part of ID

Find elements by xpath using regex

Use Scala to Find The Most Common 'uncommon' word in keys

hashing password using brcrpt in "pre"

Python Hashing Password using Function

Find the maximum elements in an array using recursion

Using XPATH to find elements with a certain attribute

Use of find elements in Selenium using Ruby

how to find the root elements name using xpath

How to find hidden elements using jquery selector

Recursively find elements in dictionary using an array in Python

Find Duplicate Elements In Array Using Swift

How to find elements without using the tag attribute?

Unable to find some html elements using Jsoup

Find all elements in ElementTree by attribute using Python

Find and replace some elements in a list using python

In R, find elements of a vector in a list using vectorization

Using SIMD to find the biggest difference of two elements

Reading DOMDocument and find elements using CSS selectors