
public int solution(int[] array)
{
int answer = 0;
int maxNum = 0;
int maxNumCount = 0;
bool doubleFlag = false;
for (int i = 0; i < array.Length; i++)
{
int tempNumCount = 0;
for (int j = 0; j < array.Length; j++)
if (array[i] == array[j])
tempNumCount++;
if (tempNumCount > maxNumCount)
{
maxNum = array[i];
maxNumCount = tempNumCount;
doubleFlag = false;
}
else if (tempNumCount == maxNumCount && array[i] != maxNum)
doubleFlag = true;
}
if (doubleFlag)
answer = -1;
else
answer = maxNum;
return answer;
}
public int solution(int[] array) {
Dictionary<int, int> counts = new Dictionary<int, int>();
foreach (int num in array) {
if (counts.ContainsKey(num)) {
counts[num]++;
} else {
counts[num] = 1;
}
}
int maxCount = 0;
int same = -1;
bool sameCheck = false;
foreach (var pair in counts) {
if (pair.Value > maxCount) {
maxCount = pair.Value;
same = pair.Key;
sameCheck = false;
} else if (pair.Value == maxCount) {
sameCheck = true;
}
}
if (sameCheck) {
return -1;
} else {
return same;
}
}




'📖TIL' 카테고리의 다른 글
| 260105 CS기초 리비전 (0) | 2026.01.05 |
|---|---|
| 260105 네트워크 (0) | 2026.01.05 |
| 251210 Loading (0) | 2025.12.10 |
| 251210 StringBuilder (0) | 2025.12.10 |
| 251210 Github Issue & Github Projects (0) | 2025.12.10 |