- #1
Vintageflow
- 5
- 0
hello all, I was given an assignment on arrays, here is the task:
Write a program that inputs an array of 10 integers from the user, and removes the duplicate array elements. You may assume that all the integers are between 0 and 100, and you may input them from the user however you wish. The easiest way to do this is to store only the non-repeating elements (only store a number the first time you see it). Please write at least 1 function in addition to the main method, and pass the array into that function as a parameter. Don't use any class-level variables. Each variable should be declared inside a method. Here is some sample output:
Please enter 10 integers, hitting return after each one:
5
75
10
75
5
80
10
5
5
50
You entered 5 unique numbers:
5 75 10 80 50
here is what my program looks like:
import java.util.Scanner;
public class DeDup
{
static Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
int[] nums;
int numbers, array;
boolean number;
System.out.print("Please enter 10 integers, hitting return after each one: ");
numbers = scan.nextInt();
nums = new int[10];
for(int count = 1; count < 10; count++)
{
nums[count] = scan.nextInt();
}
}
public static int[] numMethod()
{
int i, j=0, arraySize=10;
int[] inputNum = new int[arraySize];
for (i=0; i<arraySize; i++)
{
inputNum = scan.nextInt();
}
for (i=0; i<arraySize; i++)
{
for(j=0; j<i; j++)
{
if (inputNum == inputNum[j])
inputNum = inputNum[j-1];
j--;
}
}
System.out.print(inputNum+" ");
return(inputNum);
}
}
my first method works fine (inputing the 10 numbers using an array), but I'm having trouble on the second method, where I get rid of the duplicate numbers and only display the unique numbers. How do you get rid of the duplicates? Is the second method even right?
thank you all for the help!
Write a program that inputs an array of 10 integers from the user, and removes the duplicate array elements. You may assume that all the integers are between 0 and 100, and you may input them from the user however you wish. The easiest way to do this is to store only the non-repeating elements (only store a number the first time you see it). Please write at least 1 function in addition to the main method, and pass the array into that function as a parameter. Don't use any class-level variables. Each variable should be declared inside a method. Here is some sample output:
Please enter 10 integers, hitting return after each one:
5
75
10
75
5
80
10
5
5
50
You entered 5 unique numbers:
5 75 10 80 50
here is what my program looks like:
import java.util.Scanner;
public class DeDup
{
static Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
int[] nums;
int numbers, array;
boolean number;
System.out.print("Please enter 10 integers, hitting return after each one: ");
numbers = scan.nextInt();
nums = new int[10];
for(int count = 1; count < 10; count++)
{
nums[count] = scan.nextInt();
}
}
public static int[] numMethod()
{
int i, j=0, arraySize=10;
int[] inputNum = new int[arraySize];
for (i=0; i<arraySize; i++)
{
inputNum = scan.nextInt();
}
for (i=0; i<arraySize; i++)
{
for(j=0; j<i; j++)
{
if (inputNum == inputNum[j])
inputNum = inputNum[j-1];
j--;
}
}
System.out.print(inputNum+" ");
return(inputNum);
}
}
my first method works fine (inputing the 10 numbers using an array), but I'm having trouble on the second method, where I get rid of the duplicate numbers and only display the unique numbers. How do you get rid of the duplicates? Is the second method even right?
thank you all for the help!