int temp = 0;
int counter;
int match = 0;
int FindDup(int array[], int K, int N)
{
// Sorting the array from small big numbers.
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
// Find minimum number of K occurences
for (int i = 0; i < N; i++)
{
counter = 0;
for (int j = 0; j < N; j++)
{
if (array[i] == array[j]) // checks if array element is equal
{
counter++;
}
}
if (counter == K)
{
match = array[i];
return match;
}
}
return -1;
}
Info about this function: This function is a mix of sorting and finding minimum occurrences of an array.
Problem: The function does work as intended but needs optimization from another user for further improvements of the code. It would be great seeing someones opinions of what would be better and what could be changed.
Input:
arraySize: 10
arrayElements: 2 4 6 7 3 4 5 6 3 6
numberOfOccurrences: 2
Output: 3