5 Replies - 484 Views - Last Post: 22 March 2019 - 10:31 PM Rate Topic: -----

#1 vaterjake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3621
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 vaterjake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 765
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 ccdan   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1