Randomly generated arrays of length 100, array elements 1 to 10, and the most frequent and least-counted elements

Source: Internet
Author: User

The idea is very simple, you can read it.

Import java.util.*;
	Class Max {static int a,b,c,d,e,f,g,h,i,j;
		public static void Main (string[] args) {int[] num=new int[100];
		Random rd=new Random ();
			for (int n=0;n<100;n++) {num[n]=rd.nextint (10) +1;
			System.out.println (Num[n]);
			if (num[n]==1) a++;
			else if (num[n]==2) b++;
			else if (num[n]==3) C + +;
			else if (num[n]==4) d++;
			else if (num[n]==5) e++;
			else if (num[n]==6) f++;
			else if (num[n]==7) g++;
			else if (num[n]==8) h++;
			else if (num[n]==9) i++;
		else if (num[n]==10) j + +;
		} int[] Cn={a,b,c,d,e,f,g,h,i,j};
		Arrays.sort (CN);
			System.out.println (Arrays.tostring (CN));
			int Max=count (cn[9]);

		int Min=count (cn[0]);
		
	System.out.println ("The most frequent" is: "+max+", a total of: "+cn[9]+" times, "+" appears the least number of times: "+min+", a total appeared: "+cn[0]+" ");
			static int count (int m) {if (m==a) return 1;
			else if (m==b) return 2;
			else if (m==c) return 3;
			else if (M==d) return 4;
			else if (m==e) return 5; else if(M==F) return 6;
			else if (m==g) return 7;
			else if (m==h) return 8;
			else if (m==i) return 9;
			else if (M==J) return 10;
		else return 0;
 }
}

There is also a more technologically advanced solution that can solve similar problems. The following program provides two traversal methods for your reference. The idea is also very simple, using the HashMap, the array elements in turn and map inside the initial key comparison, if there is the same, then value plus 1.

Import java.util.*;
Class	Count 
{public
	void Count (int[] arr)
	{
		int num=0;
		Map<integer,integer> map=new hashmap<integer,integer> ();
		for (int i=1;i<=10;i++)
		{
			map.put (i,num);
		}
		for (int i=0;i<arr.length;i++)
		{
			/*iterator it = Map.entryset (). iterator ();    
			while (It.hasnext ())
				{     
					map.entry m= (map.entry) It.next ();
					if (arr[i]== (int) m.getkey ())
						map.put (int) M.getkey (), (int) m.getvalue () +1);
				} */For
				(map.entry<integer,integer> m:map.entryset ())
				{
					if (arr[i]== (int) m.getkey ()
						) Map.put ((int) M.getkey (), (int) m.getvalue () +1);
				}
		For (map.entry<integer,integer> M:map.entryset ())
		{
			System.out.println ("+m.getkey () +" appears as: "+ M.getvalue () + "Times");
		}
	
	public static void Main (string[] args) 
	{
		Random rd=new Random ();
		Int[] Arr=new int[100];
		for (int i=0;i<100;i++)
		{
			arr[i]=rd.nextint +1;
		}
		New count (). Count (arr);
	}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.