# Getting rid of duplicate numbers in list

Page 1 of 1

## 5 Replies - 484 Views - Last Post: 22 March 2019 - 10:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=415425&amp;s=64c7829e148ff059956f624b93683bc4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 vaterjake

Reputation: 0
• Posts: 11
• Joined: 26-February 19

# Getting rid of duplicate numbers in list

Posted 21 March 2019 - 08:52 AM

```public class EliminateDuplicates{
public static int[] eliminateDuplicates(int[] numbers) {
int[] temp = new int[numbers.length];
int size = 0;
for (int i = 0; i < numbers.length; i++) {
if (linearSearch(temp, numbers[i]) == -1) {

}
}
int[] result = new int[size];
for (int i = 0; i < size; i++)
result[i] = temp[i];
return result;
}
public static int linearSearch(int[] list, int key){
for(int i=0;i<list.length;i++){
if(key==list[i])
return i;
}
return -1;
}
public static void main(String[] args){
int[] numbers=new int[10];

java.util.Scanner input=new java.util.Scanner(System.in);
System.out.print("Enter ten integers: ");

for(int i=0;i<numbers.length;i++){
numbers[i]=input.nextInt();
}

int[] result=eliminateDuplicates(numbers);

System.out.println("The number of distinct integers is "+result.length);
System.out.print("The distinct integers are ");

for(int e:result)
System.out.print(e+" ");
}

}
```

I'm trying to write code that the user puts in 10 integers and gives how many distinct numbers and what those numbers are. I having trouble figuring out how to remove the duplicate numbers. I think it has to do with lines 6 to 8 unless it has to do something with linearSearch(lines 15-21). Can somebody help me what I'm doing wrong?

Is This A Good Question/Topic? 0

## Replies To: Getting rid of duplicate numbers in list

### #2 g00se

• D.I.C Lover

Reputation: 3621
• Posts: 16,639
• Joined: 20-September 08

## Re: Getting rid of duplicate numbers in list

Posted 21 March 2019 - 09:02 AM

So what is your code doing? You need to be able to explain how the algo(s) should work before you start writing code

### #3 vaterjake

Reputation: 0
• Posts: 11
• Joined: 26-February 19

## Re: Getting rid of duplicate numbers in list

Posted 21 March 2019 - 05:59 PM

When I run the program and put in ten 1's, the output is "The number of distinct integers is 0
The distinct integers are ". When putting in those numbers, I want it to compute "The number of distinct integers is 1
The distinct integers are 1".As far as I know, I'm messing up somewhere.

### #4 NormR

• D.I.C Lover

Reputation: 765
• Posts: 5,762
• Joined: 25-December 13

## Re: Getting rid of duplicate numbers in list

Posted 21 March 2019 - 06:25 PM

Can you describe the steps the program needs to take to solve the problem?
Make a list of the steps and post them so we can see what your design is.
As g00se said, you need to work out the algorithm before writing the code.

### #5 MentalFloss

• .

Reputation: 619
• Posts: 1,590
• Joined: 02-September 09

## Re: Getting rid of duplicate numbers in list

Posted 21 March 2019 - 06:54 PM

There are three choices you can pick from to solve this problem.

• Check if the currently entered number exists already; O(n)
• After all numbers have been entered, sort the list and iterate over ensuring n != n+1; probably O(n2)
• Iterate over an unsorted list for every position (for { for { } }); O(n2)

So, even though it breaks the single responsibility principles, you might favor storing each entered number only if it doesn't already exist in the list rather than collecting all numbers and then finding duplicates.

The downside is that you would be locked into that behavior. It would be required that only in initial addition of numbers could you remove duplicates. If there was some requirement that at any time someone may add another number, then it would be a bad solution.

It comes down to what you ultimately need.

### #6 ccdan

Reputation: 4
• Posts: 117
• Joined: 09-December 12

## Re: Getting rid of duplicate numbers in list

Posted 22 March 2019 - 10:31 PM

Just check out carefully lines 04, 10, 13 - you'll see that your code doesn't make any sense.

This post has been edited by ccdan: 22 March 2019 - 10:31 PM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }