Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Уймин Максим
- //IT School, 2 группа
- //09.10.2015
- //Задача 3. Найти самое часто повторяющееся значение среди элементов массива
- import java.io.PrintStream;
- import java.util.Scanner;
- public class Exersise_3 {
- public static PrintStream out = System.out;
- public static Scanner scan = new Scanner(System.in);
- public static int[] CreateArray (int N){
- //Функция создаёт массив и заполняет его случайными числами
- int[]array = new int[N];
- out.println("Original array:");
- for (int i=0;i<N;i++){
- array[i]=(-10) + (int)(Math.random()*((10-(-10))+1));
- out.print(array[i]+" ");
- }
- out.println();
- return array;
- }
- public static void CountElements (int[] array,int N){
- int[]k = new int[N];
- for (int i=0;i<N;i++){
- k[i]=0;
- for (int j=0;j<N;j++){
- if (array[i] == array[j]) k[i] = k[i]+1;
- }
- }
- int max=-2147483648;
- int m=0;
- for (int i=0;i<N;i++){
- if (k[i]>max){ max = k[i]; m=array[i];}
- }
- out.println("Required number is "+m);
- }
- public static void MainFunction (){
- //Здесь собран весь функционал программы
- out.println("Enter the array size(intger number, greather then zero)");
- int N = scan.nextInt();
- int[] array=new int [N];
- array = CreateArray(N);
- CountElements(array, N);
- }
- public static void main (String [] args){
- //Основная функция, отсюда начинается выполнение приложения
- MainFunction();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement