Repeat exactly three times in Array - C++

Hello,

I have an array with Non-fixed size and I'd like to get indexes of an array that exactly repeat three times.
For example:

We should index 3 , 6 and 7

I don't code, I need a solution

Thanks

Image:

https://i.ibb.co/y6GLy0D/Untitled.jpg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <map>
using namespace std;


map<int,int> freq( int A[], int N )
{
   map<int,int> F;
   for ( int i = 0; i < N; i++ ) F[A[i]]++;
   return F;
}


int main()
{
   int A[] = { 1, 3, 5, 7, 3, 8, 7, 7, 9 };
   int N = sizeof A / sizeof A[0];
   map<int,int> F = freq( A, N );

   int targetCount = 3;
   for ( auto p : F )
   {
      if ( p.second == targetCount )
      {
         cout << p.first << " occurs " << targetCount << " times, at indices ";
         for ( int i = 0; i < N; i++ ) if ( A[i] == p.first ) cout << i << ' ';
         cout << '\n';
      }
   }
}


7 occurs 3 times, at indices 3 6 7 
Last edited on
Registered users can post here. Sign in or register to post.