vendredi 29 avril 2016

How to find the most duplicates in an array?

I'm don't know how to find duplicates in an array. After finding the duplicates, I also need the program to figure out which duplicate to use to print the highest score (based on a formula)

e.g. input: 3 5 7 7 7 7 7 12 12 12 18 20

/* program calculating n * n * n * y = score

7 appears 5 times = 5*5*5*7 = 875

12 appears 3 times = 3*3*3*12 = 324 */

output: 7 is the highest scoring duplicate at 875.

I am restricted to using only arrays, if/else, printf/scanf, loops..

My code so far (only works for some inputs):

#include <stdio.h>

#define MAX_NUMBERS 15

int main(void){

int nNumbers, i, j;
int sum;
int selectNumber;

int array[MAX_NUMBERS]; 

//Reading the numbers
scanf("%d", &nNumbers); //Enter the number of elements in the array
for(i = 0; i < nNumbers; i++){    
    scanf("%d", &array[i]);
}

//This is the part I'm having trouble with, 
//writing conditions for it to find the highest scoring duplicate.
j = 0;
selectNumber = array[0];
for(i = 0; i <= nNumbers - 1; i++){
    selectNumber = array[i];
    for(i = 1; i <= nNumbers - 1; i++){
        if((array[i] == runNumber) && (array[0] != selectNumber)){
            array[j] = selectNumber; //^ doesn't work if 1st num is duplicate
            j++;
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire