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
31
32
33
34
#include "stdafx.h"
 
using namespace std;
 
typedef struct TEST {
    int idx;
    int count=0;
};
 
const int SIZE = 5;
const int MAZ = 4;
 
int main(void)
{
    int arr[5= { 2,2,2,3,4 };
    TEST test[5];
    int idx;
    int value;
    for (int i = 0; i < SIZE; i++)
    {        
        test[arr[i]].count += 1;
        if (test[arr[i]].count == 2)
        {
            value = arr[i];
            idx = test[arr[i]].idx;
            break;
        }
        test[arr[i]].idx = i;
    }
    
    printf("index = %d , result = %d", idx, value);
 
    return 0;
}
cs




arr 배열을 0부터 차례대로 보면서 그 수에 맞는 위치로 숫자를 보내는 거임.

예를 들어 arr[0]은 1이니까 test[1]에 들어감. 들어갔으니까 count를 +1해주고, 그 다음에 arr[0]의 인덱스인 0을 idx에 저장하는거임

그럼 중간에 if는 뭐냐? 중복수를 검출해야하니까 최초의 중복수는 test배열에서 count 1짜리들이 2로 변할 때가 중복수니까 저렇게 짠거임.