Reputation: 1
i have problem with this question
Duplicate Elimination Use a one-dimensional array to solve the following problem: Write an application that inputs 10 integers. As each number is read, display it only if it is not a duplicate of a number already read. Use the smallest possible array to solve this problem. Display the complete set of unique values input after the user inputs all values.
Sample Output:
Enter 10 integers:
12 33 67 9 10 6 6 34 10 19
Unique Values:
12 33 67 9 10 6 34 19 note the question ask to reprint the array but without any repeating number
and this is my code
import java.util.Scanner;
public class duplicate
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int[] array = new int[10];
int[] barray = new int[10];
System.out.println(" Enter 10 integers: ");
int i ;
for(i=0;i<array.length;i++)
{
array[i]= input.nextInt();
barray[i] = array[i];
}
for(i=0;i<array.length;i++)
{
System.out.printf("\t %d ",array[i]);
}
System.out.println("\n Unique values are: ");
for ( i = 0; i < array.length; i++ )
{
{
if ( array[ i ] == barray[ i ] )
break;
System.out.printf("\t %d",array[i]);
}
}
}
}
Upvotes: 0
Views: 8841
Reputation: 1
public class DuplicateElimination2 {
static Scanner input = new Scanner(System.in);
static int[] array1 = new int[10];
public static void main(String[] args) {
// loop insert 10 integer separated by space into the array.
for (int counter = 0; counter < array1.length; counter++) {
// convert string input of digit into int values
int userInput = Integer.parseInt(input.next()); // collect user input
// assign element userInput into an index of array1
array1[counter] = userInput;
}
// printing unique values
for (int i = 0; i < array1.length; i++) {
int ifNumberExist = 0; // hold the current number a value appears
// print the first index since it need not to be compared with any other index
if(i == 0)
System.out.print(array1[i] + " ");
else
// j < i means every element from array1[j] will be compare to array1[1 - i]
for (int j = 0; j < i; j++) {
if(array1[j] == array1[i]) // check if the array element are equal
++ifNumberExist; // increment if a match is found
}
// print current array1 element if no match was found
if(ifNumberExist < 1 && i > 0)
System.out.print(array1[i] + " ");
}
}
}
Upvotes: 0
Reputation: 533530
You can also try a shorter, if more obscure...
System.out.println("Unique values "+
new LinkedHashSet<String>(Arrays.asList(scanner.nextLine().split(" +"))));
Upvotes: 0
Reputation: 12334
Two things to change:
If you loop over your array for each input looking for dups. Dup? then don't insert, not dup? add to the array.
Using the smallest array possible is a trick because it depends on knowing how many dups will be input ahead of time which you may not. There are two solutions, use an array that is only big enough for the unique numbers in the assignment, or when you exceed the size of the array, create a new one and copy all the values over to it. Does the assignment mean you are limited to using one one instance of an array which happens to be single-dimensional or only that the array you use must be one-dimensional but you can use more than one array?
Upvotes: 0
Reputation: 1436
How about using a BitSet since all the values are integers.
BitSet bs = new BitSet();
for(int i=0; i<array.lenght; i++)
{
bs.set(array[i]);
}
System.out.println("Unique Values are: "+bs.toString());
Upvotes: 0
Reputation: 5796
The Arrays class http://java.sun.com/javase/6/docs/api/java/util/Arrays.html have some useful methods for this topic, like sort.
Skipping input, the loop could look like:
int[] out = new int[1];
int[] ints = new int[]{3,56,2,98,76,4,9,2,3,55};
Arrays.sort(ints);
out[0] = ints[0]; //handle first value
for(int i = 1; i < ints.length; i++){
if(ints[i-1] != ints[i]){
int[] temp = new int[out.length+1];
for(int j = 0; j < temp.length-1; j++){
temp[j] = out[j]; //copy all previous
}
temp[temp.length - 1] = ints[i]; //add last value
out = temp;
}
}
System.out.println(Arrays.toString(out));
Upvotes: 0
Reputation: 48265
Do you really need to use arrays? A Set
would be ideal here:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Set<Integer> set = new HashSet<Integer>(10);
System.out.println(" Enter 10 integers: ");
for (int i = 0; i < 10; i++) {
set.add(input.nextInt());
}
System.out.println("Unique values are: ");
for (Integer i : set) {
System.out.printf("\t %d", i);
}
}
Upvotes: 4
Reputation: 492
Have you considered your problem without focusing on code? Imagine I gave you a bit of paper and asked you to write down all the numbers I shouted out, without duplicates, then read the numbers back to me when I was done. Think how you'd solve that in the real world before writing the code. E.g. would you write down numbers twice then score them out later? Would you need 2 sheets of paper - one with duplicates and one without?
Upvotes: 1
Reputation: 17750
For your last loop, I'd consider something like this:
for(int i = 0; i < array.length; i++)
{
Boolean duplicated = false;
for(int j = 0; j<i; j++) {
if(array[i] == array[j]) {
//number already printed
duplicated = true;
break;
}
}
if(duplicated == false) //print number here.
}
It's not the best way to do it, but It follows what you already did.
The best way would be to use an adequate structure, I'm not a Java user but maybe HashSet
is the way to go.
Upvotes: 0
Reputation: 50273
Assuming that your problem is that the "print duplicates" part does not work: can you use a Vector? If so, you could do something like this when printing the unique values:
for each item in the array
if the vector does not contain the item
print the value
insert the item in the vector
Upvotes: 1