How to find duplicate in String Arrays ?

In this article, we will see how to find duplicate in String Arrays

1. Step to find duplicate in String Arrays:

  1. Create String Arrays consisting few duplicate element/objects
  2. First convert String Arrays into List
  3. And then convert List into Set, as directly converting String Arrays to Set is not possible
  4. Get length of String Arrays using length property of Arrays
  5. Similarly get size of Set/HashSet object using size() method
  6. Finally compare Arrays length with Set size using if-else statement
  7. If String Arrays length is greater-than HashSet size then there are duplicate element/objects are present in original String Arrays
  8. Else, there are NO duplicate present

DuplicateInStringArrays.java

package in.bench.resources.string.duplicate;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DuplicateInStringArrays {

	public static void main(String[] args) {

		// String Arrays
		String[] strArrays = {
				"Cabbage", 
				"Kale",
				"Radish",
				"Onions",
				"Garlic",
				"Beetroot",
				"Cucumber",
				"Carrot",
				"Kale"
		};

		// First convert to List
		List<String> list = Arrays.asList(strArrays);

		// Then convert to Set
		Set<String> set = new HashSet<String>(list);

		// check String Arrays length and HashSet size
		int strArrayLength = strArrays.length;
		int setSize = set.size();

		if(strArrayLength >= setSize) {
			System.out.println("Duplicate element/objects"
					+ " present in original String Arrays");
		}
		else {
			System.out.println("There are NO duplicates"
					+ " in original String Arrays");
		}
	}
}

Output:

Duplicate element/objects present in original String Arrays

Note : above demo example depicts whether duplicate present or not

 

2. How to get duplicate element/object from original String Arrays ?

  • In the above example, instead of converting List to Set using inter-conversion constructor, we will add each element/objects of List one-by-one to Set using add(); method
  • add(); method returns false value, when we try to add duplicate element
  • Note : But it is time-consuming

GetDuplicateElementFromStringArrays.java

package in.bench.resources.string.duplicate;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class GetDuplicateElementFromStringArrays {

	public static void main(String[] args) {

		// String Arrays
		String[] strArrays = {
				"Cabbage", 
				"Kale",
				"Radish",
				"Kale",
				"Garlic",
				"Beetroot",
				"Garlic",
				"Carrot",
				"Kale"
		};

		// First convert to List
		List<String> list = Arrays.asList(strArrays);

		// just empty HashSet object
		Set<String> set = new HashSet<String>();

		// iterate through List
		for(String str : list) {
			// add element to Set/HashSet
			boolean flagForDuplicate = set.add(str);

			if(!flagForDuplicate) {
				System.out.println(str + " is duplicate element");
			}
		}
	}
}

Output:

Kale is duplicate element
Garlic is duplicate element
Kale is duplicate element

 

3. To get frequency of duplicate element/object in String Arrays:

  • First. convert Arrays to List using Arrays.asList(arr);
  • And then convert List into Set for storing only unique element/objects
  • Now, use static frequency(); method of Collections class by passing converted List and unique element from Set
  • Repeat above step for all unique element/objects present in Set, by iterating Set
  • Finally, print result to console, while iterating Set itself

GetDuplicateFrequencyInStringArrays.java

package in.bench.resources.collection;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class GetDuplicateFrequencyInStringArrays {

	public static void main(String[] args) {

		// String Arrays
		String[] strArrays = {
				"Cabbage", 
				"Kale",
				"Radish",
				"Kale",
				"Garlic",
				"Ginger",
				"Garlic",
				"Carrot",
				"Kale"
		};

		// First convert to List
		List<String> list = Arrays.asList(strArrays);

		// just empty HashSet object
		// add converted List (Arrays -> List)
		Set<String> setVeg = new HashSet<String>(list);

		System.out.println("Element-Name\tFrequency");
		System.out.println("============\t==========");

		// Iterate using enhanced for-each loop
		for (String veg : setVeg) {
			System.out.println(veg + "\t\t"
					+ Collections.frequency(list, veg));
		}
	}
}

Output:

Element-Name	Frequency
============	==========
Carrot		1
Radish		1
Garlic		2
Cabbage		1
Ginger		1
Kale		3

 

Happy Coding !!
Happy Learning !!

Various ways to iterate through LinkedList – 5 ways
How to sort Vector using Collections.sort() in Java