0

I have 2 arrays in which arr1 stores a number (the salary) and arr2 stores a string (the employee's name). Since the two arrays are linked, I cannot change the order of arr1, or sort it. I am looking for a more efficient way to solve the problem which is to find if there are any duplicates in the array. It might be more than one duplicate, but if no are found it should print "no duplicates found".

 int count = 0;

    for (int i = 0;i<arr_size ;i++)
    {
        for (int j = 0; j < arr_size && i != j; j++)
        {
            if (arr[i] == arr[j])
            {
              cout << arr2[i] << " " << arr1[i] << endl;
              cout << arr2[j] << " " << arr1[j] << endl;
              count ++;

            }
        }   

    }
    if (count == 0)
    {
        cout << "No employee have same salaries"<<endl;
    }

I don't want to use such an inefficient way to solve the problem. Is there any better suggestion? Thanks for the help :) And the question also requires me to print out all the duplicated employee and salaries pair

0

3 Answers 3

3

You can use an unordered_set which has an average constant time insertion and retrieval:

#include <unordered_set>
// ...set up arr
int count = 0;
std::unordered_set<int> salaries;
for (int i = 0; i < arr_size; i ++) {
    if (salaries.count(arr[i]) > 0) {
        // it's a duplicate
    }
    salaries.insert(arr[i]);
}
// do more stuff
Sign up to request clarification or add additional context in comments.

3 Comments

Why this would be more efficient? unordered_set::count is log(n), not log(1) [and since it is a set, max count is 1]. Although in this you would check only against non-duplicates, you'd have to create another container and duplicate it in memory, having up to the double of the memory and costing the time to create the copies. This stills a for inside a for.
unordered_set::count is O(1) since it's a hashset, as opposed to set::count which is O(log(n)) due to being a red/black search tree set. Also, there is no boolean way to determine if an element is in the hashset, only count and find. Also, you're not duplicating any containers in memory, and there is no nested loop here.
@Aplet123: you might use result of insert instead of count to avoid extra look-up.
0

Create a Haspmap using unordered_map and store salaries and index of the salary . Now if the same salary exist then increase count

Comments

0

You can reduce the time complexity of the algorithm to O(n) by using unordered_set on the expense of using additional space.

#include<unordered_set>
int main(){
// Initialise your arrays
unordered_set<string> unique;
bool flag = false;
for(int i=0;i<arr_size;i++){
  // Since unordered_set does not support pair out of the box, we will convert the pair to string and use as a key
  string key = to_string(arr1[i]) + arr2[i];
  // Check if key exists in set
  if(unique.find(key)!=unique.end())
    unique.push(key);
  else{
    // mark that duplicate found
    flag = true;
    // Print the duplicate
    cout<<"Duplicate: "+to_string(arr1[i])+"-"+arr2[i]<<endl;
  }
}
if(!flag){
  cout<<"No duplicates found"<<endl;
} else cout<<"Duplicates found"<<endl;
return 0;
}

1 Comment

but what if i need to print out all the duplicate salaries and its employees?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.