0

This is my current attempt to remove duplicates from an array holding integers, but this does not give me any results. It simply prints out nothing. Any help would be greatly appreciated.

 public static void duplicate(int numbers[], int size)
 {
 for (int i = 0; i < size; i++){
 boolean duplicate = false;
 int b = 0;
 while (b < i){
 if (numbers[i] == numbers[b])
 duplicate = true;
 b++;}
 if (duplicate = false)
 System.out.print(numbers[i] + " ");}
 } 
asked Nov 18, 2014 at 15:41
1
  • this should work fine. just change = with == in the last if. Commented Nov 18, 2014 at 15:44

3 Answers 3

1

Try this:

public static void duplicate(int numbers[], int size)
{
 for (int i = 0; i < size; i++){
 boolean duplicate = false;
 int b = 0;
 while (b < i){
 if (numbers[i] == numbers[b])
 duplicate = true;
 b++;}
 if (duplicate == false)
 System.out.print(numbers[i] + " ");}
} 

You need to use == not = in your if statement.

answered Nov 18, 2014 at 15:42
0
0

You can also make use of HashSet or LinkedHashSet to Preserve the ordering

public void removeDupInIntArray(int[] ints){
 Set<Integer> setString = new LinkedHashSet<Integer>();
 for(int i=0;i<ints.length;i++){
 setString.add(ints[i]);
 }
 System.out.println(setString);
}
answered Nov 18, 2014 at 15:47
0

The best choice would be to use the Sets(Hash Linkedhash) so duplicates can be avoided

answered Nov 18, 2014 at 19:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.