Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default [SOLVED] need advice on this method.

    For example, consider this array have 10 elements: 65, 50, 79, 58, 83, 91, 65, 98, 79, 79
    this program outputs this
    The number of times that 65 appears in the array: 2
    Indices that 65 is located: 0 6

    The number of times that 65 appears in the array: 2
    Indices that 65 is located: 0 6

    The number of times that 79 appears in the array: 3
    Indices that 79 is located: 2 8 9

    The number of times that 79 appears in the array: 3
    Indices that 79 is located: 2 8 9


    can someone help me on what i need to change to get the program to display only one once for the same number?:confused::confused:
    Java Code:
    	import java.util.Scanner;
    		public class RedundantElements
    		{
    			public static void main (String[] args)
    			{
    		Scanner input = new Scanner(System.in);
    
    		System.out.println("Enter an integer for n, ranging 10 and 30.");
    
    			int inp = input.nextInt();
    
    			int [] array = new int[inp];
    
    			int [] newArr = assign(array);
    
    
    	System.out.println("The random values in an array with size "+array.length+" are:");
    			displaynum(newArr);
    
    			displayVaFq(newArr);
    
    
    	}
    	//assign random number to n size array
    		public static int[] assign(int[] array)
    			{
    				int [] newArr = new int[array.length];
    			for (int i =0; i<array.length; i++)
    			{
    				newArr[i] = (int)(Math.random()*51)+50;
    
    			}
    			 return newArr;
    		 }//end of assign method
    
    	//display numbers
    		 public static void displaynum(int[] array)
    		 {
    			 for (int j=0; j< array.length; j++)
    			 {
    				if ((j+1)%5==0)
    				System.out.println(array[j]+" ");
    				else
    				System.out.print(array[j]+" ");
    		 }//end of for
    	 }//end of void method
    
    	//display value and fequency
    	[COLOR="SeaGreen"]	public static void displayVaFq(int[] array)
    		{
    			int  count = 1;
    
    			int rep =0;
    
    			for (int k=0; k<array.length; k++)
    			{
    				for (int L=0; L<array.length; L++)
    					{
    						if (array[k] == array[L] && k != L)
    						{
    							count++;
    							rep = k;
    						}//end of if
    
    
    					}//end of second for
    
    					if (count >= 2)
    					{
    						System.out.println("\nThe number of times that "+ array[rep] + " appears in the array: "+count);
    						System.out.println("Indices that "+array[rep]+" is located: ");
    
    						for (int i = 0; i<array.length; i++)
    						{
    							if (array[rep] == array[i] )
    							{
    								System.out.print(" "+i+" ");
    							}
    
    
    						}
    					}//end of if
    					//clear count and rep
    					count = 1;
    					rep = 0;
    					
    					
    
    			}//end of first for[/COLOR]
    
    		}//end of displayVaFq method
    
    	}

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Java Code:
    for (int L=k+1; L<array.length; L++)

Similar Threads

  1. Need advice on JSP with bean
    By butterhero in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-18-2009, 11:40 AM
  2. JTextField add numbers advice
    By thelinuxguy in forum Advanced Java
    Replies: 9
    Last Post: 02-20-2009, 02:03 AM
  3. Some advice please!
    By awebbtt in forum New To Java
    Replies: 3
    Last Post: 02-02-2009, 07:23 PM
  4. Advice needed
    By dineshjf in forum Advanced Java
    Replies: 18
    Last Post: 01-09-2009, 03:17 AM
  5. Advice on best method for....
    By shaungoater in forum Java 2D
    Replies: 1
    Last Post: 06-23-2008, 07:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •