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] + " ");}
    } 
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.

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

Comments

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);
}

Comments

0

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

Comments

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.