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:
Search through and remove duplicate integers from an unsorted arraylist
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.
setSeedhere. Anew Random()instance will have a random (enough) seed. Check the javadocs.setSeedjust seemed to work for me. I am however still having trouble removing duplicates in my randomly populated arraylist.