Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Checking then removing duplicates in array

  1. Nov 30, 2011 #1
    Well, the title pretty much sums up my question.

    Code (Text):
             public static int[] removeDuplicates(int[] x)
            for (int i = 0; i < x.length; i++)
                for (int j = i + 1; j < x.length; i++)
                    if (x[i] == x[j])
                        System.out.print("Found a duplicate!");
    I came up with this. This should check for duplicates but the result isn't quite what I want.

    Consider this array:

    Code (Text):
    int[] array = { 1, 2, 3, 4, 5, 1, 2, 6, 1 };
    Well, this has three 1s and two 2s. I want it so it give me that we have three 1s and two 2s and removes them. How exactly do I do that?

  2. jcsd
  3. Nov 30, 2011 #2


    Staff: Mentor

    What are you supposed to do when you find a duplicate? "Remove the duplicate" is on the vague side.
  4. Nov 30, 2011 #3


    User Avatar
    Homework Helper

    If the goal is to remove duplicates, than you'd only remove two 1s and one 2. Are you still stuck on how to "remove" a duplicate?
  5. Dec 1, 2011 #4
    When a duplicate of an integer in the array is found, it should be removed from the array. Every duplicate of a number will be removed and a new array will be created without any duplicates.

    Yes, I'm sorry I wasn't clear on that. Any repeating integer will be removed (not the integer itself). Yes, I'm still stuck on how to remove the duplicate, checking for duplicates is somehow working.
  6. Dec 1, 2011 #5
    In think in java, as far as I know, adding elements(objects) at the end of an array is easy. But inserting and deleting the elements in the middle of the array is difficult. In this case, we have to re-arrange all the elements of the array.(By using some sort of sorting techniques.) Then possibly you can process the elements in your case.
    Remember that array holds elements of only one type of datatype. If you want different objects in a single array, you have to use Collections concept. You can use Collection concept even on arrays which have elements of single data type.
    Also I think, retrieving the elements from an array is easy but after retrieving the elements, if we want to process them, then there are no methods in java to carry this out.
    The alternative or the only way could be to use wrapper classes to convert each element in an array to an object and use a 'different object' to store all of these objects. This 'different object' is called Collection object.The Collection object actually stores the references of other objects, not the physical copies of other objects. Collection object never stores primitive data type, stores only objects.That is why there is a concept called Wrapper classes introduced. Collection object belongs to Collection class. Collection class belongs to java utility package.
    There are several Collection classes in java utility package to process these elements(objects). We are advised to use these classes at our discretion. We have to decide which Collection class(generic class) is best suited to process our requirement/problem.
    I think you should go through the concepts of Collections thoroughly.
    You should be familiar with Type casting, Wrapper classes and Collections.
    Last edited: Dec 1, 2011
  7. Dec 1, 2011 #6


    User Avatar
    Science Advisor
    Homework Helper

    There are (at least) two different ways to get rid of duplicates.

    A. Delete them from the existing array somehow.
    B. Create a new array, and only copy the non-duplicates into it.

    If you don't like "plan A", try working on "plan B" instead.
  8. Dec 1, 2011 #7


    User Avatar
    Homework Helper

    Since you're stuck here, the most common method to remove a duplicate is to move the remaining values in the array down one position, so that the value to be removed is overwritten by the first of the remaining values. After doing this you need to consider the size of the array as reduced by 1. So now the question is how would you move the remaining values down one position within the array?
  9. Dec 1, 2011 #8
    You have already figured out how to, for each item in the array, find all duplicates of that item further in the array.
    But instead you need to figure out how to determine whether or not an item should be added to the new array. When should an item not be added to the duplicate free array, if it already has some items in it?

    "Plan B" probably better for you. It should be simpler than "Plan A" to implement. Also, typically many less operations will be required, so we'd expect "Plan B" to execute faster. The memory savings from using "Plan A," (an "in-place" algorithm) is not significant in this case.
  10. Dec 2, 2011 #9
    Suppose I want to find the duplicates in an array but I don't know how many elements I am going to give to the array. And while I enter the values it should display that I have entered duplicate elements. Here, I want to know how do I accept array elements dynamically. If it's not possible in array. Then is there any way in java that I can accept elements dynamically from the keyboard during run-time and so that I can prompt the user to enter a different value if the user entered a duplicate value?
    Main question is: I want to enter elements during run-time in java and I don't know how many elements I am going to enter. How do I do that?
  11. Dec 2, 2011 #10


    User Avatar
    Science Advisor

    Hiche, try moving all the duplicates to the end of the array.

    If you don't need to resize the array, I would recommend you do the above approach.
  12. Dec 15, 2011 #11
    The problem could be solved with a Binary search tree.
    A standard textbook implementation drops duplicate values on insertion.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook