Remove and Replace Duplicates in ArrayList
<p>How do I remove duplicate numbers on ArrayList and replace them with new ones?</p>

<p>I want to print the numbers without them duplicating.</p>

<p>This is my code:</p>

<pre><code>import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JOptionPane;

public class Test {
    public static void main(String[] args) {
        ArrayList&lt;Integer&gt; al = new ArrayList&lt;Integer&gt;();

        int opt = Integer.parseInt(JOptionPane.showInputDialog("How many numbers?");
        for (int i=0 ; i &lt; opc ; i++) {
            al.add(Integer.parseInt(JOptionPane.showInputDialog("Which numbers?")));
        }

        Set&lt;Integer&gt; s = new HashSet&lt;&gt;();
        for (Integer d : al){
            if (s.add(d) == false)
                JOptionPane.showMessageDialog(null,"The number " + d + " was duplicated in position " + al.lastIndexOf(d));
                JOptionPane.showMessageDialog(null,"Replace new number"); //This is where I would like to replace the numbers if possible
            }
        JOptionPane.showMessageDialog("Your numbers without duplicates: "); //This is where it would print
        }
    }
}
</code></pre>