-3

I am trying to find duplicate element in an array. I have already solved it using traversing the array. But now i want to convert array to arraylist and use contains keyword of arraylist.

I am not able to convert array to arraylist and use the contains keyword. please look in following code:

public void findDuplicate2() {
    int arr[] = { 1, 2, 3, 4, 5, 3, 7, 5 };
    ArrayList<int[]> arrlist = new ArrayList<>(Arrays.asList(arr));

    for (i = 0; i < len; i++) {
        if (arrlist.contains(arr[i]))
            System.out.println(arr[i]);
        else
            System.out.println("no duplicate" + arr[i]);
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161

3 Answers3

1

There are a number of problems with your code:

  • You are creating an ArrayList<int[]>, that is, an array list of arrays of int. This list contains one array, namely the one you started out from. I don’t think this was what you intended. My guess is you wanted an ArrayList<Integer> containing the numbers from the array. khelwood’s link should help you out.
  • You haven’t declared i nor len. I suggest you use for (int i = 0; i < arr.length; i++).
  • arrlist.contains(arr[i]) always returns false since a list of arrays cannot contain a number (the numbers inside the array are not searched). It’s a design problem with ArrayList that you can ask for whether it contains an element of the wrong type (there are historical reasons for this). If you change the arraylist to ArrayList<Integer> as I suggested above, it will work, though.
  • Once you get the above to work, arrlist.contains(arr[i]) will always return true since each number from the array will be in the array list, also the ones that are not duplicates.
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thanks, 1. Yes true I wanted to create arraylist of integer which will have same element as in array. 2. I changed the element to integer before as well but i am getting compile time error (Type mismatch: cannot convert from ArrayList to ArrayList) 3. len is length of array which i had declared where array was declared. while posting here i forgot to copy that. Need help with above compile time error – Rajesh Chaudhary Mar 02 '17 at 15:24
  • `Arrays.asList()` does not work with an array of primitives (like `int`s). This is the reason for your compile time error. To convert your array of `int` to `ArrayList` you may either see [How to convert int[\] into List in Java?](http://stackoverflow.com/questions/1073919/how-to-convert-int-into-listinteger-in-java) (as has been mentioned) or use `ArrayList arrlist = new ArrayList<>(Arrays.stream(arr).boxed().collect(Collectors.toList()));` (requires Java 8). – Ole V.V. Mar 02 '17 at 22:08
  • 1
    This solved my problem. – Rajesh Chaudhary Mar 03 '17 at 01:21
  • hey Ole V.V what if i have array of string in this case? suppose String arr = "somerandom". Can i change this to arraylist? – Rajesh Chaudhary Mar 09 '17 at 10:13
  • Of course you can. How? That depends on whether you mean a `String` or an array (`String[]`), and the element type you want in your `ArrayList`. Please post a new question and spell out your requirements and also what you have tried and how it failed. I promise I will take a look. – Ole V.V. Mar 09 '17 at 10:36
0

Here your arrlist variable is of type int[]. Here your arrlist size is 1(the entire input array as single entry).

So, you can only check the existence of any integer array in the list by using contains method of arrlist. if (arrlist.contains(arr)) //returns true in your case

SatishV
  • 393
  • 4
  • 22
  • if (arrlist.contains(arr)) will always return true. How can i create an arraylist (integer type ) with same element which are present in array? – Rajesh Chaudhary Mar 02 '17 at 15:33
0

An easier way to find dublicates is this one:

public void findDuplicate2(){
    int arr[] = {1,2,3,4,5,3,7,5};

    for (int i = 0; i < arr.length; i++) {
        boolean dublicate = false;
        for (int j = 0; j < arr.length; j++) {
            if(arr[i] == arr[j] && i != j){
                System.out.println(arr[i]);
                dublicate = true;
                break;
            }
        }
        if(!dublicate)
            System.out.println("No dublicate " + arr[i]);
    }
}

In your code the arraylist contains one element, the array arr. You can check this with

System.out.println(arrlist.get(0));
System.out.println(arrlist.get(1));

The right way would be to transfer each element of arr to arraylist...

Abgehter
  • 151
  • 1
  • 9