Find Duplicate Value in Array List and Print the Duplicated Value

Find duplicate value in array list and print the duplicated value

If you want to reduce some time complexity at the cost of O(n) space, you can use a Set

Set<String> set = new HashSet<>();
for(int i =0; i < list.size(); i++) {
if (set.contains(list.get(i))) {
System.out.println(list.get(i)+" is duplicated");
} else set.add(list.get(i));
}

find duplicate element in array list

You can simply utilize ArrayList#contains to verify if an element already exists within the List.

public boolean addCourse(String course) {
if (courses.contains(course)) {
return false;
}

return courses.add(course);
}

Identify duplicates in a List

The method add of Set returns a boolean whether a value already exists (true if it does not exist, false if it already exists, see Set documentation).

So just iterate through all the values:

public Set<Integer> findDuplicates(List<Integer> listContainingDuplicates) { 
final Set<Integer> setToReturn = new HashSet<>();
final Set<Integer> set1 = new HashSet<>();

for (Integer yourInt : listContainingDuplicates) {
if (!set1.add(yourInt)) {
setToReturn.add(yourInt);
}
}
return setToReturn;
}

Java: Detect duplicates in ArrayList?

Simplest: dump the whole collection into a Set (using the Set(Collection) constructor or Set.addAll), then see if the Set has the same size as the ArrayList.

List<Integer> list = ...;
Set<Integer> set = new HashSet<Integer>(list);

if(set.size() < list.size()){
/* There are duplicates */
}

Update: If I'm understanding your question correctly, you have a 2d array of Block, as in

Block table[][];

and you want to detect if any row of them has duplicates?

In that case, I could do the following, assuming that Block implements "equals" and "hashCode" correctly:

for (Block[] row : table) {
Set set = new HashSet<Block>();
for (Block cell : row) {
set.add(cell);
}
if (set.size() < 6) { //has duplicate
}
}

I'm not 100% sure of that for syntax, so it might be safer to write it as

for (int i = 0; i < 6; i++) {
Set set = new HashSet<Block>();
for (int j = 0; j < 6; j++)
set.add(table[i][j]);
...

Set.add returns a boolean false if the item being added is already in the set, so you could even short circuit and bale out on any add that returns false if all you want to know is whether there are any duplicates.

What is the most efficient way to find and print the duplicate values in arraylist?

The method add of Set returns a boolean whether a value already exists (true if it does not exist, false if it already exists.

So just iterate through all the values:

public Set<Integer> findDuplicates(List<Integer> listContainingDuplicates)
{
final Set<Integer> setToReturn = new HashSet();
final Set<Integer> set1 = new HashSet();

for (Integer yourInt : listContainingDuplicates)
{
if (!set1.add(yourInt))
{
setToReturn.add(yourInt);
}
}
return setToReturn;
}

How to count duplicate elements in ArrayList?

List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("a");
list.add("a");
list.add("a");

int countA=Collections.frequency(list, "a");
int countB=Collections.frequency(list, "b");
int countC=Collections.frequency(list, "c");

Finding duplicate values in arraylist

Create a comparator:

public class CarComparator implements Comparator<Car>
{
public int compare(Car c1, Car c2)
{
return c1.carName.compareTo(c2.carName);
}
}

Now add all the cars of the ArrayList to a SortedSet, preferably TreeSet; if there are duplicates add to the list of duplicates:

List<Car> duplicates = new ArrayList<Car>();
Set<Car> carSet = new TreeSet<Car>(new CarComparator());
for(Car c : originalCarList)
{
if(!carSet.add(c))
{
duplicates.add(c);
}
}

Finally in your duplicates you will have all the duplicates.

How do I find the duplicates in a list and create another list with them?

To remove duplicates use set(a). To print duplicates, something like:

a = [1,2,3,2,1,5,6,5,5,5]

import collections
print([item for item, count in collections.Counter(a).items() if count > 1])

## [1, 2, 5]

Note that Counter is not particularly efficient (timings) and probably overkill here. set will perform better. This code computes a list of unique elements in the source order:

seen = set()
uniq = []
for x in a:
if x not in seen:
uniq.append(x)
seen.add(x)

or, more concisely:

seen = set()
uniq = [x for x in a if x not in seen and not seen.add(x)]

I don't recommend the latter style, because it is not obvious what not seen.add(x) is doing (the set add() method always returns None, hence the need for not).

To compute the list of duplicated elements without libraries:

seen = set()
dupes = []

for x in a:
if x in seen:
dupes.append(x)
else:
seen.add(x)

or, more concisely:

seen = set()
dupes = [x for x in a if x in seen or seen.add(x)]

If list elements are not hashable, you cannot use sets/dicts and have to resort to a quadratic time solution (compare each with each). For example:

a = [[1], [2], [3], [1], [5], [3]]

no_dupes = [x for n, x in enumerate(a) if x not in a[:n]]
print no_dupes # [[1], [2], [3], [5]]

dupes = [x for n, x in enumerate(a) if x in a[:n]]
print dupes # [[1], [3]]

ArrayList of objects, compare objects and find duplicates, add object to new ArrayList only once for each duplicate

As I pointed out in the comments, you can simply check for the presence of one student before adding it:

ArrayList<Student> allStudentsA = assignStudents();
ArrayList<Student> allStudentsB = allStudentsA;

for (Student studentA : allStudentsA)
for (Student studentB : allStudentsB)
if (studentA.getId().equals(studentB.getId())
&& studentA.getEduNumber() != studentB.getEduNumber())
if (!duplicateStudents.contains(studentB))
duplicateStudents.add(studentB);

Note that this will only work if you overrode the equals and hashCode method of your Student class, as the objects do not have the same references.

Basically, you will check if the Student is already in the list before adding it. If you implemented correctly your equals method, a student A won't be equal to A with a different priority .



Related Topics



Leave a reply



Submit