Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding index of duplicate values in an ArrayList

Tags:

java

arraylist

I have an ArrayList which contains duplicate values at diff diff index. for example {"Indian","American","Chinese","Australian","Indian","Russian","Indian"} as u can see the value - "Indian" exists at index - 0, 4 & 6.

I need to know all these indexes where "Indian" exists and create an arrayList of that. Here is my code:

public void filter(){


    categoryArray = Arrays.asList(category);

    for(String k : category){
        //Log.v("filter", filterTerm);
        if(k.equals(filterTerm.toLowerCase()))
        {               
            int p = categoryArray.indexOf(k);                   
            Log.v("index of categArr", ""+p);
            String id = Integer.toString(p);
            indexes.add(id);


        }// end of if
    }// end of for

Here I get how many times duplicate occurs by getting the size of indexes(ArrayList) but when I check the values . Its one value at all index since in the method : indexOf() it always brings the index of first value that it finds in the Array.

So if duplicate exists at index - 2,5,7 I get the array size of index as 3. But the values are {2,2,2,};

like image 284
Ankit Avatar asked Apr 22 '11 10:04

Ankit


People also ask

How do I find the index of a duplicate element in a list?

Method #1 : Using loop + set() In this, we just insert all the elements in set and then compare each element's existence in actual list. If it's the second occurrence or more, then index is added in result list.

How do you find the index of an ArrayList?

The index of a particular element in an ArrayList can be obtained by using the method java. util. ArrayList. indexOf().

Can we get index from value in ArrayList in Java?

indexOf() in Java. The indexOf() method of ArrayList returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.


2 Answers

This is a situation where an index-based for loop is more appropriate than enhanced for loop that you're using, as what you need to grab is the index.

You can base all your work on the original array rather than converting it to a list, and I suspect you were going for case-insensitive match.

public void filter(){
    for(int i=0; i<category.length; i++){
        if(category[i].equalsIgnoreCase(filterTerm))
        {               
            String id = Integer.toString(i);
            indexes.add(id);
        }
    }
}

If you have an ArrayList rather than an array, of course similar code will work, but using list.get(i) instead of category[i].

like image 155
Don Roby Avatar answered Sep 28 '22 17:09

Don Roby


You need to know which index in the array you are currently at, not the first index where it is to be found. To keep track of that, put

int i = 0;

before the loop, and at the very end of the loop put

i++;

Then the variable i tells you where you have found the value, so you can add i to the indexes list.

like image 27
Robin Green Avatar answered Sep 28 '22 18:09

Robin Green