-1

Relevant but a different problem than this: https://stackoverflow.com/a/58474681/12174978

in this question, I have learned that how I can find the first and second repetitive elements of a variable using a map. I'm thinking to work with a for loop that will generate repetitive numbers using a defined function.

#include <iostream>
using namespace std;

int addition(int a, int b)
{
    int r;
    if (b < 6)
    {
        r = a;
    }
    else {
        r = a + b;
    }
    return r;
}

int main()
{
    int z;
    int a = 1;
    for (int b = 0; b < 15; b++)
    {
        z = addition(a, b);
        cout << "The result is " << z << "\n" << endl;

    }
}

From the above code what I have got is some repetitive 1 and other numbers as well (1,1,1,1,... 9,10, 11...16). So I would like to get only the first two same numbers position and printout.

If I change the above code we might get the output element different where the repetition would be different too. But I always want to consider the only first two repetitive of the same number.

For example, if the repetition in the print out like this: 6,3,4,5,5,5,5,6,7,6,

I want to consider only the first two 6 positions which are in [0], [7] place, and for 5, the first two are in [3], and [4] place.

I tried to do this in the way I mentioned in the link, but what I have ended up is I was getting the same position multiple times.

UPDATE: The reason I would like to get the repetition of the first time and second time of a number because I would like to selectively use that number and position as well in my analysis.

So my expected output would be only the elements that are more than once. I would like to get their position as well, so I can know which one came in the for loop and which one second

t.niese
  • 39,256
  • 9
  • 74
  • 101
zero_field
  • 335
  • 1
  • 11
  • Looks like you copied the wrong code to your question. – t.niese Oct 23 '19 at 05:04
  • Actually no. In the code, I gave an example of how the repetition would look like. and later I explained, how else we will be able to get if we change the code. I have updated the question though, please let me know if that needs more clarification. – zero_field Oct 23 '19 at 05:18
  • The shown code has a loop that prints the result of `addition`, but I can't see any attempt where you try to solve the problem you describe? – t.niese Oct 23 '19 at 05:23
  • I did not post my attempts of the code because I'm trying to make that run in the compiler, I'm still having some bugs, and I could not run the code yet. – zero_field Oct 23 '19 at 05:28
  • you want the first and second positions for each unique number present in the list? – srt1104 Oct 23 '19 at 06:42
  • That's right. The reason I would like to get the repetition of the first time and second time of a number, because I would like to selectively use that number and position as well in my analysis. – zero_field Oct 23 '19 at 06:54
  • What overall problem are you trying to solve? It's extremely difficult to decipher what you're asking to do here. – Ayjay Oct 23 '19 at 07:20
  • `I did not post my attempts of the code because I'm trying to make that run in the compiler, I'm still having some bugs, and I could not run the code yet.` and how do you know that your attempt does not work if you can even compile it? And how should we be able to tell why your attempt does nolt work if we don't see it? In the current form, the question just asks others to do all the work for you, and that is not what StackOverflow is about. – t.niese Oct 23 '19 at 08:01

1 Answers1

0

You can use a map to associate each value with the indexes in the input sequence. Example:

#include <iostream>
#include <vector>
#include <map>
using namespace std;

int main() {
    vector v = { 6,3,4,5,5,5,5,6,7,6 };
    using index_t = int;
    map<int, vector<index_t>> positions;
    for (int i = 0; i < v.size(); ++i) {
        positions[v[i]].push_back(i);
    }

    // operate on 6s
    for (auto index : positions[6]) {
        std::cout << "6 is at position:" << index << '\n';
    }

    // operate on 5s
    for (auto index : positions[5]) {
        std::cout << "5 is at position:" << index << '\n';
    }
}
Ayjay
  • 3,413
  • 15
  • 20
  • Ayjay, but I don't have a vector right? I'm getting the result from function : z = addition(a, b); I also have updated the question and commented in the last for you so you would understand what I'm looking for. – zero_field Oct 23 '19 at 07:35
  • Just add whatever values you want to the map, you don't have to use a vector: `for (int b = 0; b < 15; b++) positions[addition(a, b)].push_back(b);` will work also. – Ayjay Oct 23 '19 at 07:52