R. Ivanis
R. Ivanis

Reputation: 15

Java -- error in removing duplicate elements from array

I know some people have asked about duplicates already, and I've looked through all those answers but none seem to help me/ I still can't get it to compile. Is there something obvious in my code that I am missing???? Help (new to programming!)

I need to write a method that takes one argument, an array of String values, and returns a new array that has no duplicate values and no need to order the original array. For example

String [] abc = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
String [] new = removeDuplicates (abc);
for (int i = 0; i < new.length; i++)
{
    System.out.print (new[i] + " ");
}
System.out.println();

The ouput should be: A C B

Here is my code:

    import java.util.*;

    class Duplication
    {
        public static void main (String [] args)
        {
            String [] values = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
            String [] newList = removeDuplicates (values);
        }
        private static String removeDuplicates (String [] originalArray)
        {
            for (int i = 0; i < newList.length; i++)
            {
                System.out.print (newList[i] + " ");
            }
            System.out.println();

            String noDuplicates = originalArray.length;
            for (int d = 0; d < noDuplicates; d++)
            {
                for (int e = d + 1; e < noDuplicates; e++)
                {
                    if(originalArray[d] == originalArray[e])
                    {
                        originalArray[e] = originalArray[noDuplicates-1];
                        noDuplicates--;
                        e--;
                    }
                }
            }
        }  
    }

Six errors:

    Duplication.java:9: error: incompatible types
    String [] newList = removeDuplicates (values);
                                     ^
    required: String[]
    found:    String
    Duplication.java:14: error: cannot find symbol
    for (int i = 0; i < newList.length; i++)
                        ^
    symbol:   variable newList
    location: class Duplicates
    Duplication.java:16: error: cannot find symbol
        System.out.print (newList[i] + " ");
                          ^
    symbol:   variable newList
    location: class Duplicates
    Duplication.java:21: error: incompatible types
    String noDuplicates = originalArray.length;
                                       ^
    required: String
    found:    int
    Duplication.java:32: error: bad operand types for binary operator '-'
                originalArray[e] = originalArray[noDuplicates-1];
                                                             ^
    first type:  String
    second type: int
    Duplication.java:35: error: bad operand type String for unary operator '--'
                noDuplicates--;
                            ^

Upvotes: 0

Views: 518

Answers (4)

minus
minus

Reputation: 2786

You should read the error messages.

 Duplication.java:9: error: incompatible types
 String [] newList = removeDuplicates (values);

This function return String which is not String []

 private static String removeDuplicates (String [] originalArray)

You should declared it as

 private static String [] removeDuplicates (String [] originalArray)

You should declare newList within the scope of the function to use it, this causes

Duplication.java:14: error: cannot find symbol
for (int i = 0; i < newList.length; i++)

Lengths are integer and should be treated as such like:

int noDuplicates = originalArray.length;

Read carefully the compiling errors, they address all the problems.

Upvotes: 1

cactuschibre
cactuschibre

Reputation: 2375

1) Your method removeDuplicates () return a String. In your main block, you are expecting an array of String.

2) The scope variable newList is inside the main block. So, this variable is not visible in the method removeDuplicates

3) "originalArray.length" return an "int", not a String.

Upvotes: 1

strash
strash

Reputation: 1321

Thats all the code you need. You can read for the collections here https://docs.oracle.com/javase/tutorial/collections/

import java.util.*;

    class Duplication
    {
        static String [] values = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
        public static void main (String [] args){
            System.out.println(removeDuplicates(values));

        }
        static Set removeDuplicates(String [] originalArray)
        {
            List arList = new ArrayList<String>(Arrays.asList(values));
            return new LinkedHashSet<String>(arList);
        }  
    }

Upvotes: 0

This is invalid in java:

public static void main (String [] args)
String [] values = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
String [] newList = removeDuplicates (values);

you have to add the { } to define the scope of the static main method:

public static void main (String [] args){
      String[] values = {"A", "C", "C", "B", "A", "C", "B", "B", "A"};
      String[] newList = removeDuplicates (values);
}

Upvotes: 1

Related Questions