0

I am working on a Java Program to generate a random ArrayList populated with integers from 1-25 inclusive. I then needed to make a copy of the array and store it in a separate location. While I was able to accomplish these portions, I am having trouble implementing a way to:

  1. Search through and remove duplicate integers from an unsorted arraylist

  2. Store all duplicates removed

Here is my program so far:

public static void main(String[] args) {
    ArrayList <Integer> OgArr = new ArrayList <Integer>();
    
    Random random = new Random();
    
    random.setSeed(System.currentTimeMillis());
    
    int n = 35;
    
    for (int i = 0; i < n; i++)
    {
        Integer r = 1 + random.nextInt(25);
        OgArr.add(r);
    }
    
    ArrayList<Integer> copyOfArr = (ArrayList<Integer>) OgArr.clone();
    
    int i = 0;
    int leng = copyOfArr.size();
    
    for (i = 0; i < leng - 1; i++) {
        int imin = i;
        for (int j = i + 1; j < leng; j++) {
            if (copyOfArr.get(j) < copyOfArr.get(imin)) {
                imin = j;
            }
        }
        Collections.swap(copyOfArr,i,imin);      
    }
    System.out.println(copyOfArr);
}

This program does the first two functions and I have explored using a temporary array, for loops, and a LinkedHashSet. I was unsuccessful in my efforts and I would really appreciate assistance in implementing this.

7
  • 1
    You don't need to use setSeed here. A new Random() instance will have a random (enough) seed. Check the javadocs. Commented May 11, 2022 at 3:23
  • @StephenC thank you for your comment. I was reading documentation and various different sources about how to approach these sorting and searching programs. setSeed just seemed to work for me. I am however still having trouble removing duplicates in my randomly populated arraylist. Commented May 11, 2022 at 3:38
  • @StephenC should I maybe sort the arraylist and then remove duplicates in the array? I just didn't know which way I should go first. Commented May 11, 2022 at 3:39
  • 1
    1) setSeed works. It is just unnecessary. read the Javadoc. 2) Please read my answer. Specifically the hints. Sorting is not necessary (and in fact it is wrong because the requirements imply that the list should remain unsorted.). Commented May 11, 2022 at 3:53
  • Thank you. Do you have a small example I could take a look at? Commented May 11, 2022 at 11:18

1 Answer 1

1

I cannot figure out what your "mental model" is for doing tasks 1 and 2 from your code. Indeed the related code in the question doesn't make sense to me. I think you should probably throw most of it away and start again.

If you do ... I recommend that you first think through what the code needs to do. Explain this to yourself by writing some pseudo-code. Then rewrite the pseudo-code in Java.

Here are a couple of hints to get you thinking in the right direction:

  1. You can use the optional Iterator.remove method to remove an element while iterating. Read the javadoc for Iterator.

  2. Alternatively you could create a new list and copy elements one at a time leaving out those that should not be there.

  3. You can use a HashSet<Integer> to identify elements that you have seen before; i.e. duplicates. Read the javadoc for the add method. Specifically, what the return value means.


Update

Since you are really struggling, here is some pseudo-code. Read it, understand it, then translate it to working code, using the above hints:

   set seen = ... // empty set
   list duplicates = ...// empty list
   list newList = ... // new empty list
   for element in originalList {
       // Test if 'element' has been 'seen' before
       // If no, add 'element' to 'newList' and add `element` to `seen`
       // If yes, add 'element' to 'duplicates'
   }

And as I said a few times before find and read the javadocs.


Finally, there is more than one way to solve this, and some are more efficient than others. But your goal should be to figure our a working solution with as little help as possible. Strive for "working" and "code that you understand".

Sign up to request clarification or add additional context in comments.

1 Comment

Note for others who are tempted: This is a learning exercise in basic Java programming ... not an exercise in optimization, or how to use streams or something. Writing a potted solution is unlikely to help the OP to learn to program. And surely, that is the real goal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.