Removing Duplicate Integers in an ArrayList









up vote
0
down vote

favorite












I see a lot of posts with the same topic (mostly with Strings) but haven't found the answer to my question. How would I remove duplicate integers from an ArrayList?



import java.util.*;

public class ArrayList2
public static ArrayList<Integer> removeAllDuplicates(ArrayList<Integer> list)
Collections.sort(list);
for (int i = 0; i < list.size(); i++)
if (list.get(i) == list.get(i + 1))
list.remove(i);


return list;




This is the start of my code, the only problem that has arised is that if there are 3 integers with the same value, it only removes one of them. If I put in 4, it removes two of them. PLEASE NO HASHING!!!



The ArrayList and the output when I run it:



List: [-13, -13, -6, -3, 0, 1, 1, 1, 5, 7, 9]
Duplicates Removed: [-13, -6, -3, 0, 1, 1, 5, 7, 9]


This is my first time using this website, so please let me know if I'm doing something wrong with formatting/if there's already an answer to my question that I missed.










share|improve this question



















  • 7




    Any specific reason for using a List over a Set?
    – Mehdi B.
    Nov 8 at 23:20






  • 5




    PLEASE NO HASHING!!! Why?
    – Elliott Frisch
    Nov 8 at 23:22






  • 2




    Would it be more efficient to use a set in this situation? Also, @Micha Wiedenmann, I'll be sure to do that.
    – Excel
    Nov 8 at 23:22







  • 1




    @Excel A Set guarantees uniqueness, so yeah, probably - but I'd avoid been tempted to micro-optimise the solution at this point. It would be simpler, easier and more likely to work
    – MadProgrammer
    Nov 8 at 23:23







  • 1




    new HashSet<>(list) or list.stream().distinct().collect(Collectors.toList())
    – Kartik
    Nov 8 at 23:24














up vote
0
down vote

favorite












I see a lot of posts with the same topic (mostly with Strings) but haven't found the answer to my question. How would I remove duplicate integers from an ArrayList?



import java.util.*;

public class ArrayList2
public static ArrayList<Integer> removeAllDuplicates(ArrayList<Integer> list)
Collections.sort(list);
for (int i = 0; i < list.size(); i++)
if (list.get(i) == list.get(i + 1))
list.remove(i);


return list;




This is the start of my code, the only problem that has arised is that if there are 3 integers with the same value, it only removes one of them. If I put in 4, it removes two of them. PLEASE NO HASHING!!!



The ArrayList and the output when I run it:



List: [-13, -13, -6, -3, 0, 1, 1, 1, 5, 7, 9]
Duplicates Removed: [-13, -6, -3, 0, 1, 1, 5, 7, 9]


This is my first time using this website, so please let me know if I'm doing something wrong with formatting/if there's already an answer to my question that I missed.










share|improve this question



















  • 7




    Any specific reason for using a List over a Set?
    – Mehdi B.
    Nov 8 at 23:20






  • 5




    PLEASE NO HASHING!!! Why?
    – Elliott Frisch
    Nov 8 at 23:22






  • 2




    Would it be more efficient to use a set in this situation? Also, @Micha Wiedenmann, I'll be sure to do that.
    – Excel
    Nov 8 at 23:22







  • 1




    @Excel A Set guarantees uniqueness, so yeah, probably - but I'd avoid been tempted to micro-optimise the solution at this point. It would be simpler, easier and more likely to work
    – MadProgrammer
    Nov 8 at 23:23







  • 1




    new HashSet<>(list) or list.stream().distinct().collect(Collectors.toList())
    – Kartik
    Nov 8 at 23:24












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I see a lot of posts with the same topic (mostly with Strings) but haven't found the answer to my question. How would I remove duplicate integers from an ArrayList?



import java.util.*;

public class ArrayList2
public static ArrayList<Integer> removeAllDuplicates(ArrayList<Integer> list)
Collections.sort(list);
for (int i = 0; i < list.size(); i++)
if (list.get(i) == list.get(i + 1))
list.remove(i);


return list;




This is the start of my code, the only problem that has arised is that if there are 3 integers with the same value, it only removes one of them. If I put in 4, it removes two of them. PLEASE NO HASHING!!!



The ArrayList and the output when I run it:



List: [-13, -13, -6, -3, 0, 1, 1, 1, 5, 7, 9]
Duplicates Removed: [-13, -6, -3, 0, 1, 1, 5, 7, 9]


This is my first time using this website, so please let me know if I'm doing something wrong with formatting/if there's already an answer to my question that I missed.










share|improve this question















I see a lot of posts with the same topic (mostly with Strings) but haven't found the answer to my question. How would I remove duplicate integers from an ArrayList?



import java.util.*;

public class ArrayList2
public static ArrayList<Integer> removeAllDuplicates(ArrayList<Integer> list)
Collections.sort(list);
for (int i = 0; i < list.size(); i++)
if (list.get(i) == list.get(i + 1))
list.remove(i);


return list;




This is the start of my code, the only problem that has arised is that if there are 3 integers with the same value, it only removes one of them. If I put in 4, it removes two of them. PLEASE NO HASHING!!!



The ArrayList and the output when I run it:



List: [-13, -13, -6, -3, 0, 1, 1, 1, 5, 7, 9]
Duplicates Removed: [-13, -6, -3, 0, 1, 1, 5, 7, 9]


This is my first time using this website, so please let me know if I'm doing something wrong with formatting/if there's already an answer to my question that I missed.







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 4:31

























asked Nov 8 at 23:18









Excel

478




478







  • 7




    Any specific reason for using a List over a Set?
    – Mehdi B.
    Nov 8 at 23:20






  • 5




    PLEASE NO HASHING!!! Why?
    – Elliott Frisch
    Nov 8 at 23:22






  • 2




    Would it be more efficient to use a set in this situation? Also, @Micha Wiedenmann, I'll be sure to do that.
    – Excel
    Nov 8 at 23:22







  • 1




    @Excel A Set guarantees uniqueness, so yeah, probably - but I'd avoid been tempted to micro-optimise the solution at this point. It would be simpler, easier and more likely to work
    – MadProgrammer
    Nov 8 at 23:23







  • 1




    new HashSet<>(list) or list.stream().distinct().collect(Collectors.toList())
    – Kartik
    Nov 8 at 23:24












  • 7




    Any specific reason for using a List over a Set?
    – Mehdi B.
    Nov 8 at 23:20






  • 5




    PLEASE NO HASHING!!! Why?
    – Elliott Frisch
    Nov 8 at 23:22






  • 2




    Would it be more efficient to use a set in this situation? Also, @Micha Wiedenmann, I'll be sure to do that.
    – Excel
    Nov 8 at 23:22







  • 1




    @Excel A Set guarantees uniqueness, so yeah, probably - but I'd avoid been tempted to micro-optimise the solution at this point. It would be simpler, easier and more likely to work
    – MadProgrammer
    Nov 8 at 23:23







  • 1




    new HashSet<>(list) or list.stream().distinct().collect(Collectors.toList())
    – Kartik
    Nov 8 at 23:24







7




7




Any specific reason for using a List over a Set?
– Mehdi B.
Nov 8 at 23:20




Any specific reason for using a List over a Set?
– Mehdi B.
Nov 8 at 23:20




5




5




PLEASE NO HASHING!!! Why?
– Elliott Frisch
Nov 8 at 23:22




PLEASE NO HASHING!!! Why?
– Elliott Frisch
Nov 8 at 23:22




2




2




Would it be more efficient to use a set in this situation? Also, @Micha Wiedenmann, I'll be sure to do that.
– Excel
Nov 8 at 23:22





Would it be more efficient to use a set in this situation? Also, @Micha Wiedenmann, I'll be sure to do that.
– Excel
Nov 8 at 23:22





1




1




@Excel A Set guarantees uniqueness, so yeah, probably - but I'd avoid been tempted to micro-optimise the solution at this point. It would be simpler, easier and more likely to work
– MadProgrammer
Nov 8 at 23:23





@Excel A Set guarantees uniqueness, so yeah, probably - but I'd avoid been tempted to micro-optimise the solution at this point. It would be simpler, easier and more likely to work
– MadProgrammer
Nov 8 at 23:23





1




1




new HashSet<>(list) or list.stream().distinct().collect(Collectors.toList())
– Kartik
Nov 8 at 23:24




new HashSet<>(list) or list.stream().distinct().collect(Collectors.toList())
– Kartik
Nov 8 at 23:24












4 Answers
4






active

oldest

votes

















up vote
3
down vote



accepted










The specific reason why your removeAllDuplicates function doesn't work is that you are still iterating after a successful comparison. If you iterate only when list.get(i) != list.get(i + 1), you will get rid of all the duplicates.



public static ArrayList<Integer> removeAllDuplicates(ArrayList<Integer> list) 
Collections.sort(list);
int i = 0;
while(i < list.size() - 1)
if (list.get(i) == list.get(i + 1))
list.remove(i);
else
i++;


return list;



It's worth noting that the above function is not as fast as it could be. Though the iteration runs quickly enough, the most significant step will be the sort operation (O(n log n)).



To avoid this extra time complexity, consider using a HashSet instead of an ArrayList (if it still fits within the constraints of your problem).






share|improve this answer
















  • 1




    Checks out! Thank you, I've been stuck on this for a quick minute.
    – Excel
    Nov 8 at 23:33

















up vote
2
down vote













Other people have "answered" the basic issue, of the if statement skipping over elements because the for-loop is incrementing the index position, while the size of the array is shrinking.



This is just "another" possible solution. Personally, I don't like mutating Lists in loops and prefer to use iterators, something like...



Collections.sort(list);
Iterator<Integer> it = list.iterator();
Integer last = null;
while (it.hasNext())
Integer next = it.next();
if (next == last)
it.remove();

last = next;



Still, I think some kind of Set would be a simpler and easier solution (and since you'd not have to sort the list, more efficient ;))






share|improve this answer






















  • Whether you use an iterator or not, are you not still mutating the array within a loop?
    – unmrshl
    Nov 8 at 23:35











  • Is the list assumed to be already sorted?
    – mettleap
    Nov 8 at 23:35











  • @mettleap Yes, sorry, didn't put that in :P
    – MadProgrammer
    Nov 8 at 23:36






  • 1




    @unmrshl Not sure what you mean. The iterator is a proxy over the List, it just helps remove the possible "mutation" issues around manipulating Lists within arrays ... or am I thinking of some other constraint. In any case, no forgetting to update the index value or weird manipulation logic for the index. It's just "another" way to achieve the result, which for me, is a little cleaner. (ps - I'm think of for-each loops :P)
    – MadProgrammer
    Nov 8 at 23:36











  • You're welcome :) ... without a data structure like a Set, this is pretty efficient
    – mettleap
    Nov 8 at 23:59

















up vote
0
down vote













This assumes that you cannot use a set ("Please No hashing!!!") for a reason such as homework.



Look what happens when you remove a duplicate. Let's say that the dupe you're removing is the 1st of 3 consecutive equal numbers.



 v
-13, -6, -3, 0, 1, 1, 1, 5, 7, 9


Here i is 4 to refer to the first of the 3 1 values. When you remove it, all subsequent elements get shifted down, so that the second 1 value now takes the place of the first 1 value at index i = 4. Then the current iteration ends, another one begins, and i is now 5.



 v
-13, -6, -3, 0, 1, 1, 5, 7, 9


But now i is referring to the second of the two 1 values left, and the next element 5 isn't equal, so no dupe is found.



Every time you find a duplicate, you must decrease i by 1 so that you can stay on the first element that is a duplicate, to catch 3 or more duplicates in a row.



 v
-13, -6, -3, 0, 1, 1, 5, 7, 9


Now consecutive elements still match, and another 1 value will get removed.






share|improve this answer
















  • 1




    Would a simple "i--;" in the if statement fix the code? I'm assuming this is what you're saying.
    – Excel
    Nov 8 at 23:29











  • @Excel You "really" want to avoid messing with the iterator of a for-loop
    – MadProgrammer
    Nov 8 at 23:31










  • @Excel: It would, but there'd be a bit more work to do...it's also a faux-pas to mess with the iterator of a loop as MadProgrammer alluded to.
    – Makoto
    Nov 8 at 23:36

















up vote
0
down vote













You'd want to have a double for loop as you dont want to check the only the next index but all indexes. As well as remember when you remove a value you want to check that removed value again as it could of been replaced with another duplicate value.






share|improve this answer




















  • This would be true if he were not sorting the array. Since it is sorted, the only potential duplicates will be at an index greater than i, which can be handled by simply not iterating after each removal
    – unmrshl
    Nov 8 at 23:34










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217612%2fremoving-duplicate-integers-in-an-arraylist%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










The specific reason why your removeAllDuplicates function doesn't work is that you are still iterating after a successful comparison. If you iterate only when list.get(i) != list.get(i + 1), you will get rid of all the duplicates.



public static ArrayList<Integer> removeAllDuplicates(ArrayList<Integer> list) 
Collections.sort(list);
int i = 0;
while(i < list.size() - 1)
if (list.get(i) == list.get(i + 1))
list.remove(i);
else
i++;


return list;



It's worth noting that the above function is not as fast as it could be. Though the iteration runs quickly enough, the most significant step will be the sort operation (O(n log n)).



To avoid this extra time complexity, consider using a HashSet instead of an ArrayList (if it still fits within the constraints of your problem).






share|improve this answer
















  • 1




    Checks out! Thank you, I've been stuck on this for a quick minute.
    – Excel
    Nov 8 at 23:33














up vote
3
down vote



accepted










The specific reason why your removeAllDuplicates function doesn't work is that you are still iterating after a successful comparison. If you iterate only when list.get(i) != list.get(i + 1), you will get rid of all the duplicates.



public static ArrayList<Integer> removeAllDuplicates(ArrayList<Integer> list) 
Collections.sort(list);
int i = 0;
while(i < list.size() - 1)
if (list.get(i) == list.get(i + 1))
list.remove(i);
else
i++;


return list;



It's worth noting that the above function is not as fast as it could be. Though the iteration runs quickly enough, the most significant step will be the sort operation (O(n log n)).



To avoid this extra time complexity, consider using a HashSet instead of an ArrayList (if it still fits within the constraints of your problem).






share|improve this answer
















  • 1




    Checks out! Thank you, I've been stuck on this for a quick minute.
    – Excel
    Nov 8 at 23:33












up vote
3
down vote



accepted







up vote
3
down vote



accepted






The specific reason why your removeAllDuplicates function doesn't work is that you are still iterating after a successful comparison. If you iterate only when list.get(i) != list.get(i + 1), you will get rid of all the duplicates.



public static ArrayList<Integer> removeAllDuplicates(ArrayList<Integer> list) 
Collections.sort(list);
int i = 0;
while(i < list.size() - 1)
if (list.get(i) == list.get(i + 1))
list.remove(i);
else
i++;


return list;



It's worth noting that the above function is not as fast as it could be. Though the iteration runs quickly enough, the most significant step will be the sort operation (O(n log n)).



To avoid this extra time complexity, consider using a HashSet instead of an ArrayList (if it still fits within the constraints of your problem).






share|improve this answer












The specific reason why your removeAllDuplicates function doesn't work is that you are still iterating after a successful comparison. If you iterate only when list.get(i) != list.get(i + 1), you will get rid of all the duplicates.



public static ArrayList<Integer> removeAllDuplicates(ArrayList<Integer> list) 
Collections.sort(list);
int i = 0;
while(i < list.size() - 1)
if (list.get(i) == list.get(i + 1))
list.remove(i);
else
i++;


return list;



It's worth noting that the above function is not as fast as it could be. Though the iteration runs quickly enough, the most significant step will be the sort operation (O(n log n)).



To avoid this extra time complexity, consider using a HashSet instead of an ArrayList (if it still fits within the constraints of your problem).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 23:28









unmrshl

400110




400110







  • 1




    Checks out! Thank you, I've been stuck on this for a quick minute.
    – Excel
    Nov 8 at 23:33












  • 1




    Checks out! Thank you, I've been stuck on this for a quick minute.
    – Excel
    Nov 8 at 23:33







1




1




Checks out! Thank you, I've been stuck on this for a quick minute.
– Excel
Nov 8 at 23:33




Checks out! Thank you, I've been stuck on this for a quick minute.
– Excel
Nov 8 at 23:33












up vote
2
down vote













Other people have "answered" the basic issue, of the if statement skipping over elements because the for-loop is incrementing the index position, while the size of the array is shrinking.



This is just "another" possible solution. Personally, I don't like mutating Lists in loops and prefer to use iterators, something like...



Collections.sort(list);
Iterator<Integer> it = list.iterator();
Integer last = null;
while (it.hasNext())
Integer next = it.next();
if (next == last)
it.remove();

last = next;



Still, I think some kind of Set would be a simpler and easier solution (and since you'd not have to sort the list, more efficient ;))






share|improve this answer






















  • Whether you use an iterator or not, are you not still mutating the array within a loop?
    – unmrshl
    Nov 8 at 23:35











  • Is the list assumed to be already sorted?
    – mettleap
    Nov 8 at 23:35











  • @mettleap Yes, sorry, didn't put that in :P
    – MadProgrammer
    Nov 8 at 23:36






  • 1




    @unmrshl Not sure what you mean. The iterator is a proxy over the List, it just helps remove the possible "mutation" issues around manipulating Lists within arrays ... or am I thinking of some other constraint. In any case, no forgetting to update the index value or weird manipulation logic for the index. It's just "another" way to achieve the result, which for me, is a little cleaner. (ps - I'm think of for-each loops :P)
    – MadProgrammer
    Nov 8 at 23:36











  • You're welcome :) ... without a data structure like a Set, this is pretty efficient
    – mettleap
    Nov 8 at 23:59














up vote
2
down vote













Other people have "answered" the basic issue, of the if statement skipping over elements because the for-loop is incrementing the index position, while the size of the array is shrinking.



This is just "another" possible solution. Personally, I don't like mutating Lists in loops and prefer to use iterators, something like...



Collections.sort(list);
Iterator<Integer> it = list.iterator();
Integer last = null;
while (it.hasNext())
Integer next = it.next();
if (next == last)
it.remove();

last = next;



Still, I think some kind of Set would be a simpler and easier solution (and since you'd not have to sort the list, more efficient ;))






share|improve this answer






















  • Whether you use an iterator or not, are you not still mutating the array within a loop?
    – unmrshl
    Nov 8 at 23:35











  • Is the list assumed to be already sorted?
    – mettleap
    Nov 8 at 23:35











  • @mettleap Yes, sorry, didn't put that in :P
    – MadProgrammer
    Nov 8 at 23:36






  • 1




    @unmrshl Not sure what you mean. The iterator is a proxy over the List, it just helps remove the possible "mutation" issues around manipulating Lists within arrays ... or am I thinking of some other constraint. In any case, no forgetting to update the index value or weird manipulation logic for the index. It's just "another" way to achieve the result, which for me, is a little cleaner. (ps - I'm think of for-each loops :P)
    – MadProgrammer
    Nov 8 at 23:36











  • You're welcome :) ... without a data structure like a Set, this is pretty efficient
    – mettleap
    Nov 8 at 23:59












up vote
2
down vote










up vote
2
down vote









Other people have "answered" the basic issue, of the if statement skipping over elements because the for-loop is incrementing the index position, while the size of the array is shrinking.



This is just "another" possible solution. Personally, I don't like mutating Lists in loops and prefer to use iterators, something like...



Collections.sort(list);
Iterator<Integer> it = list.iterator();
Integer last = null;
while (it.hasNext())
Integer next = it.next();
if (next == last)
it.remove();

last = next;



Still, I think some kind of Set would be a simpler and easier solution (and since you'd not have to sort the list, more efficient ;))






share|improve this answer














Other people have "answered" the basic issue, of the if statement skipping over elements because the for-loop is incrementing the index position, while the size of the array is shrinking.



This is just "another" possible solution. Personally, I don't like mutating Lists in loops and prefer to use iterators, something like...



Collections.sort(list);
Iterator<Integer> it = list.iterator();
Integer last = null;
while (it.hasNext())
Integer next = it.next();
if (next == last)
it.remove();

last = next;



Still, I think some kind of Set would be a simpler and easier solution (and since you'd not have to sort the list, more efficient ;))







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 23:42


























community wiki





3 revs
MadProgrammer












  • Whether you use an iterator or not, are you not still mutating the array within a loop?
    – unmrshl
    Nov 8 at 23:35











  • Is the list assumed to be already sorted?
    – mettleap
    Nov 8 at 23:35











  • @mettleap Yes, sorry, didn't put that in :P
    – MadProgrammer
    Nov 8 at 23:36






  • 1




    @unmrshl Not sure what you mean. The iterator is a proxy over the List, it just helps remove the possible "mutation" issues around manipulating Lists within arrays ... or am I thinking of some other constraint. In any case, no forgetting to update the index value or weird manipulation logic for the index. It's just "another" way to achieve the result, which for me, is a little cleaner. (ps - I'm think of for-each loops :P)
    – MadProgrammer
    Nov 8 at 23:36











  • You're welcome :) ... without a data structure like a Set, this is pretty efficient
    – mettleap
    Nov 8 at 23:59
















  • Whether you use an iterator or not, are you not still mutating the array within a loop?
    – unmrshl
    Nov 8 at 23:35











  • Is the list assumed to be already sorted?
    – mettleap
    Nov 8 at 23:35











  • @mettleap Yes, sorry, didn't put that in :P
    – MadProgrammer
    Nov 8 at 23:36






  • 1




    @unmrshl Not sure what you mean. The iterator is a proxy over the List, it just helps remove the possible "mutation" issues around manipulating Lists within arrays ... or am I thinking of some other constraint. In any case, no forgetting to update the index value or weird manipulation logic for the index. It's just "another" way to achieve the result, which for me, is a little cleaner. (ps - I'm think of for-each loops :P)
    – MadProgrammer
    Nov 8 at 23:36











  • You're welcome :) ... without a data structure like a Set, this is pretty efficient
    – mettleap
    Nov 8 at 23:59















Whether you use an iterator or not, are you not still mutating the array within a loop?
– unmrshl
Nov 8 at 23:35





Whether you use an iterator or not, are you not still mutating the array within a loop?
– unmrshl
Nov 8 at 23:35













Is the list assumed to be already sorted?
– mettleap
Nov 8 at 23:35





Is the list assumed to be already sorted?
– mettleap
Nov 8 at 23:35













@mettleap Yes, sorry, didn't put that in :P
– MadProgrammer
Nov 8 at 23:36




@mettleap Yes, sorry, didn't put that in :P
– MadProgrammer
Nov 8 at 23:36




1




1




@unmrshl Not sure what you mean. The iterator is a proxy over the List, it just helps remove the possible "mutation" issues around manipulating Lists within arrays ... or am I thinking of some other constraint. In any case, no forgetting to update the index value or weird manipulation logic for the index. It's just "another" way to achieve the result, which for me, is a little cleaner. (ps - I'm think of for-each loops :P)
– MadProgrammer
Nov 8 at 23:36





@unmrshl Not sure what you mean. The iterator is a proxy over the List, it just helps remove the possible "mutation" issues around manipulating Lists within arrays ... or am I thinking of some other constraint. In any case, no forgetting to update the index value or weird manipulation logic for the index. It's just "another" way to achieve the result, which for me, is a little cleaner. (ps - I'm think of for-each loops :P)
– MadProgrammer
Nov 8 at 23:36













You're welcome :) ... without a data structure like a Set, this is pretty efficient
– mettleap
Nov 8 at 23:59




You're welcome :) ... without a data structure like a Set, this is pretty efficient
– mettleap
Nov 8 at 23:59










up vote
0
down vote













This assumes that you cannot use a set ("Please No hashing!!!") for a reason such as homework.



Look what happens when you remove a duplicate. Let's say that the dupe you're removing is the 1st of 3 consecutive equal numbers.



 v
-13, -6, -3, 0, 1, 1, 1, 5, 7, 9


Here i is 4 to refer to the first of the 3 1 values. When you remove it, all subsequent elements get shifted down, so that the second 1 value now takes the place of the first 1 value at index i = 4. Then the current iteration ends, another one begins, and i is now 5.



 v
-13, -6, -3, 0, 1, 1, 5, 7, 9


But now i is referring to the second of the two 1 values left, and the next element 5 isn't equal, so no dupe is found.



Every time you find a duplicate, you must decrease i by 1 so that you can stay on the first element that is a duplicate, to catch 3 or more duplicates in a row.



 v
-13, -6, -3, 0, 1, 1, 5, 7, 9


Now consecutive elements still match, and another 1 value will get removed.






share|improve this answer
















  • 1




    Would a simple "i--;" in the if statement fix the code? I'm assuming this is what you're saying.
    – Excel
    Nov 8 at 23:29











  • @Excel You "really" want to avoid messing with the iterator of a for-loop
    – MadProgrammer
    Nov 8 at 23:31










  • @Excel: It would, but there'd be a bit more work to do...it's also a faux-pas to mess with the iterator of a loop as MadProgrammer alluded to.
    – Makoto
    Nov 8 at 23:36














up vote
0
down vote













This assumes that you cannot use a set ("Please No hashing!!!") for a reason such as homework.



Look what happens when you remove a duplicate. Let's say that the dupe you're removing is the 1st of 3 consecutive equal numbers.



 v
-13, -6, -3, 0, 1, 1, 1, 5, 7, 9


Here i is 4 to refer to the first of the 3 1 values. When you remove it, all subsequent elements get shifted down, so that the second 1 value now takes the place of the first 1 value at index i = 4. Then the current iteration ends, another one begins, and i is now 5.



 v
-13, -6, -3, 0, 1, 1, 5, 7, 9


But now i is referring to the second of the two 1 values left, and the next element 5 isn't equal, so no dupe is found.



Every time you find a duplicate, you must decrease i by 1 so that you can stay on the first element that is a duplicate, to catch 3 or more duplicates in a row.



 v
-13, -6, -3, 0, 1, 1, 5, 7, 9


Now consecutive elements still match, and another 1 value will get removed.






share|improve this answer
















  • 1




    Would a simple "i--;" in the if statement fix the code? I'm assuming this is what you're saying.
    – Excel
    Nov 8 at 23:29











  • @Excel You "really" want to avoid messing with the iterator of a for-loop
    – MadProgrammer
    Nov 8 at 23:31










  • @Excel: It would, but there'd be a bit more work to do...it's also a faux-pas to mess with the iterator of a loop as MadProgrammer alluded to.
    – Makoto
    Nov 8 at 23:36












up vote
0
down vote










up vote
0
down vote









This assumes that you cannot use a set ("Please No hashing!!!") for a reason such as homework.



Look what happens when you remove a duplicate. Let's say that the dupe you're removing is the 1st of 3 consecutive equal numbers.



 v
-13, -6, -3, 0, 1, 1, 1, 5, 7, 9


Here i is 4 to refer to the first of the 3 1 values. When you remove it, all subsequent elements get shifted down, so that the second 1 value now takes the place of the first 1 value at index i = 4. Then the current iteration ends, another one begins, and i is now 5.



 v
-13, -6, -3, 0, 1, 1, 5, 7, 9


But now i is referring to the second of the two 1 values left, and the next element 5 isn't equal, so no dupe is found.



Every time you find a duplicate, you must decrease i by 1 so that you can stay on the first element that is a duplicate, to catch 3 or more duplicates in a row.



 v
-13, -6, -3, 0, 1, 1, 5, 7, 9


Now consecutive elements still match, and another 1 value will get removed.






share|improve this answer












This assumes that you cannot use a set ("Please No hashing!!!") for a reason such as homework.



Look what happens when you remove a duplicate. Let's say that the dupe you're removing is the 1st of 3 consecutive equal numbers.



 v
-13, -6, -3, 0, 1, 1, 1, 5, 7, 9


Here i is 4 to refer to the first of the 3 1 values. When you remove it, all subsequent elements get shifted down, so that the second 1 value now takes the place of the first 1 value at index i = 4. Then the current iteration ends, another one begins, and i is now 5.



 v
-13, -6, -3, 0, 1, 1, 5, 7, 9


But now i is referring to the second of the two 1 values left, and the next element 5 isn't equal, so no dupe is found.



Every time you find a duplicate, you must decrease i by 1 so that you can stay on the first element that is a duplicate, to catch 3 or more duplicates in a row.



 v
-13, -6, -3, 0, 1, 1, 5, 7, 9


Now consecutive elements still match, and another 1 value will get removed.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 23:26









rgettman

147k21202286




147k21202286







  • 1




    Would a simple "i--;" in the if statement fix the code? I'm assuming this is what you're saying.
    – Excel
    Nov 8 at 23:29











  • @Excel You "really" want to avoid messing with the iterator of a for-loop
    – MadProgrammer
    Nov 8 at 23:31










  • @Excel: It would, but there'd be a bit more work to do...it's also a faux-pas to mess with the iterator of a loop as MadProgrammer alluded to.
    – Makoto
    Nov 8 at 23:36












  • 1




    Would a simple "i--;" in the if statement fix the code? I'm assuming this is what you're saying.
    – Excel
    Nov 8 at 23:29











  • @Excel You "really" want to avoid messing with the iterator of a for-loop
    – MadProgrammer
    Nov 8 at 23:31










  • @Excel: It would, but there'd be a bit more work to do...it's also a faux-pas to mess with the iterator of a loop as MadProgrammer alluded to.
    – Makoto
    Nov 8 at 23:36







1




1




Would a simple "i--;" in the if statement fix the code? I'm assuming this is what you're saying.
– Excel
Nov 8 at 23:29





Would a simple "i--;" in the if statement fix the code? I'm assuming this is what you're saying.
– Excel
Nov 8 at 23:29













@Excel You "really" want to avoid messing with the iterator of a for-loop
– MadProgrammer
Nov 8 at 23:31




@Excel You "really" want to avoid messing with the iterator of a for-loop
– MadProgrammer
Nov 8 at 23:31












@Excel: It would, but there'd be a bit more work to do...it's also a faux-pas to mess with the iterator of a loop as MadProgrammer alluded to.
– Makoto
Nov 8 at 23:36




@Excel: It would, but there'd be a bit more work to do...it's also a faux-pas to mess with the iterator of a loop as MadProgrammer alluded to.
– Makoto
Nov 8 at 23:36










up vote
0
down vote













You'd want to have a double for loop as you dont want to check the only the next index but all indexes. As well as remember when you remove a value you want to check that removed value again as it could of been replaced with another duplicate value.






share|improve this answer




















  • This would be true if he were not sorting the array. Since it is sorted, the only potential duplicates will be at an index greater than i, which can be handled by simply not iterating after each removal
    – unmrshl
    Nov 8 at 23:34














up vote
0
down vote













You'd want to have a double for loop as you dont want to check the only the next index but all indexes. As well as remember when you remove a value you want to check that removed value again as it could of been replaced with another duplicate value.






share|improve this answer




















  • This would be true if he were not sorting the array. Since it is sorted, the only potential duplicates will be at an index greater than i, which can be handled by simply not iterating after each removal
    – unmrshl
    Nov 8 at 23:34












up vote
0
down vote










up vote
0
down vote









You'd want to have a double for loop as you dont want to check the only the next index but all indexes. As well as remember when you remove a value you want to check that removed value again as it could of been replaced with another duplicate value.






share|improve this answer












You'd want to have a double for loop as you dont want to check the only the next index but all indexes. As well as remember when you remove a value you want to check that removed value again as it could of been replaced with another duplicate value.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 23:30









joshau

718




718











  • This would be true if he were not sorting the array. Since it is sorted, the only potential duplicates will be at an index greater than i, which can be handled by simply not iterating after each removal
    – unmrshl
    Nov 8 at 23:34
















  • This would be true if he were not sorting the array. Since it is sorted, the only potential duplicates will be at an index greater than i, which can be handled by simply not iterating after each removal
    – unmrshl
    Nov 8 at 23:34















This would be true if he were not sorting the array. Since it is sorted, the only potential duplicates will be at an index greater than i, which can be handled by simply not iterating after each removal
– unmrshl
Nov 8 at 23:34




This would be true if he were not sorting the array. Since it is sorted, the only potential duplicates will be at an index greater than i, which can be handled by simply not iterating after each removal
– unmrshl
Nov 8 at 23:34

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53217612%2fremoving-duplicate-integers-in-an-arraylist%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo