[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:
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
}