Last active
December 17, 2015 15:29
-
-
Save ckob/5c6fe8aa6133e5efd261 to your computer and use it in GitHub Desktop.
Exercici frequent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Random; | |
| public class Program { | |
| public static void main (String args[]) { | |
| int[] arr = rndArrInt(100000, -100, 100); // Retorna un array de llargaria 100000 amb nombres aleatoris de -100 a 100 | |
| System.out.println(frequent(arr)); | |
| } | |
| public static int[] rndArrInt (int llarg, int min, int max) { | |
| Random rand = new Random(); | |
| int[] arr = new int[llarg]; | |
| for (int i=0; i<llarg; i++) { | |
| arr[i] = rand.nextInt((max - min) + 1) + min; | |
| } | |
| return arr; | |
| } | |
| public static int frequent (int[] arr) { | |
| int maxFreq = arr[0]; | |
| int sumMaxFreq = 0; | |
| int sumEvaluant = 0; | |
| boolean pasar = false; | |
| for (int i=0; i<arr.length; i++) { | |
| pasar = false; | |
| for (int x=0; x<i; x++) { | |
| if (arr[i]==arr[x]){ | |
| pasar=true; | |
| break; | |
| } | |
| } | |
| if (pasar) continue; | |
| // | |
| sumEvaluant = 0; | |
| for (int x=i; x<arr.length; x++) { | |
| if (arr[i] == arr[x]) sumEvaluant++; | |
| } | |
| if (sumEvaluant>sumMaxFreq){ | |
| sumMaxFreq = sumEvaluant; | |
| maxFreq = arr[i]; | |
| } | |
| } | |
| return maxFreq; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment