1

I am trying to count the number of duplicates when the user enters a number.

For example, the user enter this sequence:

i/p  : 1,3,3,4,4,4,5,5,5 

The output should be:

o/p :  3 

Here's what I have:


#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int countduplicate(vector<int> number)
{
    
   std::set<int> setuniquenumber(number.begin(),number.end());
   std::vector<int>(setuniquenumber.begin(),setuniquenumber.end());
   
}

int main()
{
  
   
}

Live program link: https://onlinegdb.com/Byetbj_MWD

I'm using this link as reference.

Thank you for your help.

stateMachine
  • 2,554
  • 2
  • 9
  • 19
Rahul
  • 51
  • 7
  • 2
    Can you use `std::map`? Sounds like a job for a dictionary-like container. – Andy Aug 01 '20 at 05:25
  • @Andy can I use a swap function? – Rahul Aug 01 '20 at 05:27
  • @Rahul: Where exactly are you stuck? – Bill Lynch Aug 01 '20 at 05:28
  • @BillLynch ```std::vector(setuniquenumber.begin(),setuniquenumber.end());``` here I am stuck actually because swap does not contain the IntelliSense what I want to when user enter the same number multiple time then I want to count the duplicate number – Rahul Aug 01 '20 at 05:31
  • 1
    Rahul, you can use just about anything. `#include ` included the entire C++ Standard library. Go nuts. – user4581301 Aug 01 '20 at 05:37
  • I think his mission is: use a `vector` to hold a collection of numbers, then iterate through the numbers and count how many unique numbers there are in the vector. The part i don't get is he keeps talking about `swap`, but i can't for the life of me figure out where you'd ever want to `swap` in this problem. – Andy Aug 01 '20 at 05:37
  • @Andy as you said "The part i don't get is he keeps talking about swap, but i can't for the life of me figure out where you'd ever want to swap in this problem." that is i am only trying – Rahul Aug 01 '20 at 05:42
  • @Andy now I am trying to how to return the unique number which holds the vector – Rahul Aug 01 '20 at 05:44
  • The vector `std::vector(setuniquenumber.begin(),setuniquenumber.end());` (which needs a name, btw) contains the same elements as `setuniquenumber`, you don't need it. Probably you also don't need the `set`. But I don't really see how this approach should work out in the end. A `map` might be better. Or you sort the vector and iterate through it, counting as you go along. – churill Aug 01 '20 at 06:03
  • [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) – Jesper Juhl Aug 01 '20 at 06:29
  • @churill thanks for the suggestion I will try still my program is incomplete – Rahul Aug 01 '20 at 06:41

3 Answers3

1

If you are allowed to go the std::map route, you could do something like this (this may be a tad overkill, and by the looks of your example code, you want to use set instead):

#include <iostream>
#include <vector>
#include <set>
#include <map>

int countduplicate(const std::vector<int>& number) {
    std::map<int, int> m;
    int countDupes = 0;
    
    for (std::vector<int>::const_iterator it = number.begin(); it != number.end(); ++it){
        const int currentNumber = *it;
        if(m.find(currentNumber) != m.end()){
            if(m[currentNumber] == 1){
                ++countDupes;
                ++m[currentNumber];
            }
        }else{
            m[currentNumber] = 1;
        }
    }
    
    return countDupes;
}

int main() {
    const std::vector<int> vect { 1,3,3,4,4,4,5,5,5 };
    const int dupeCount = countduplicate(vect);
    std::cout << dupeCount;

    return 0;
}

Try it out: https://onlinegdb.com/BypCzxQWD

Andy
  • 10,110
  • 3
  • 28
  • 47
  • This answer is flat wrong. It just counts the number of different integers. But not the duplicates. Please delete or modify your answer. – Armin Montigny Aug 01 '20 at 07:44
  • No, but I did. I will upvote however, if the answer get's fixed. Please try it with OPs example, you will get 4, instead of the required 3. – churill Aug 01 '20 at 09:21
  • @ArminMontigny -- for some reason i thought the question was "unique" numbers. I had just helped someone with this same problem but with unique numbers, so that was on the brain. – Andy Aug 01 '20 at 13:51
1

It is not fully clear, if your values in the std::vector are sorted or allowed to be sorted. This makes a difference.

Anyway, I will show you 2 solutions.

First. We will sort the values in the vector. Then we will iterate over all values. We will increase the duplicate counter, if the current value is equal to the previous value and if we did not increase the counter for this value already.

This we will determine with a simple boolean comparison of the previous and current value.

Very simple and straightforward.


The second solution, and this is my prefereed solution, uses the standard approach for such questions. I will count all occurences of different integers. For that you can use a std::map or std::unordered_map. The important part here is the functionality of the index operator.

This

Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.

With counter[i]++;, either the value i exists already and its counter will be incremented, or, if the value i was not existing before, an entry will be created, and the coounter incremented.

At the ned, we will just count all counters with a value > 1, because thats the meaning of or dupe in this context.

Please see:

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>

int countduplicate1(std::vector<int> number) {

    // Sort a vector, so that all duplicates are adjacent
    std::sort(number.begin(), number.end());

    // The result. Counter for duplicates
    int counter{};
    bool newNumber{ true };

    // Go through all digits and check, if duplicate
    for (size_t i{}; i < number.size(); ++i) {

        // If duplicate, but was not already counted before
        if ((i > 0) && newNumber && (number[i] == number[i - 1]))
            ++counter;
        newNumber = ((i > 0) && (number[i] != number[i - 1]));
    }
    return counter;
}

int countduplicate2(std::vector<int> number) {
    // Counter for occurence of an integer
    std::map<int, size_t> counter{};

    // Count all different integer
    for (int i : number) counter[i]++;

    // Duplicate means that an integer is avaliable more than once
    return std::count_if(counter.begin(), counter.end(), [](const std::pair<int, size_t>& c) {return c.second > 1; });
}

int main() {

    std::vector<int> vect{ 1,3,3,4,4,4,7,9,9,9,8 };

    std::cout << countduplicate1(vect) << '\n';
    std::cout << countduplicate2(vect) << '\n';

    return 0;
}
Armin Montigny
  • 7,879
  • 3
  • 11
  • 29
  • thanks for the answer why you write > 1 this line not understood ```std::count_if(counter.begin(), counter.end(), [](const std::pair& c) {return c.second > 1; });``` – Rahul Aug 01 '20 at 09:24
  • I have written > 1 to find duplicates. If a number is onyl available once (like the number 1 in above example) then it is not duplicate. A certain number is only duplicate, if it appears at least 2 times (counter is > 1). – Armin Montigny Aug 01 '20 at 09:38
0

As already suggested in the comments, you can use a std::map<int, int> where the key is some int-value and the mapped value is the number of occurences of the key.

Algorithm / Pseudo code:

  1. for each value i in number:
    1. if the map contains key i, then increase the mapped value by one
    2. else add the key-value-pair with key i and value 1
  2. start the counter n of duplicate numbers at 0
  3. for each key-value-pair [key, value]:
    1. if value is > 1, then key had some duplicates, increse the counter n by one
    2. else key was unique in number and we can ignore it

Implementation specific hints:

  • for a std::map<int, int> m steps 1.1 and 1.2 can be combined to m[i]++, this will automatically add the key if it doesn't exist already

  • you can literally iterate over key-value-pairs like this:

    for(auto [key, value] : m) {...}
    
  • steps 2 and 3 can be combined by using std::count_if

Actual implementation is left to the reader as exercise.

churill
  • 9,299
  • 3
  • 13
  • 23
  • Hm, 3 hours after I answered the question, you give basically the same answer. No code, but pseudo code. For exactly, what I did. With 2 or 3 lines of C++ code. Then your last sentence . . . All very strange . . . – Armin Montigny Aug 02 '20 at 05:32