Java – How to find duplicate in String[] Arrays ?

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

Finding duplicates in String[] Arrays :

  1. Check duplicates present or not ?
  2. Get duplicates elements from String[] Arrays
  3. Get frequency of duplicate elements
  4. Get frequency of duplicate elements along with index-position

1. Step to find duplicate in String[] Array :

  • Create String[] Arrays consisting few duplicate element/objects
  • First convert String[] Arrays into List
  • And then convert List into Set, as directly converting String Arrays to Set is not possible
  • Get length of String Arrays using length property of Arrays
  • Similarly get size of Set/HashSet object using size() method
  • Finally compare Arrays length with Set size using if-else statement
  • If String Arrays length is greater-than HashSet size then there are duplicate element/objects are present in original String Arrays
  • 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 Array ?

  • 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 Array :

  • 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

4. To get frequency & index-position of duplicate elements in an Array :

  • First. convert Arrays to List using Arrays.asList(arr);
  • Create temporary HashSet to store unique elements of List
  • Iterate through List using traditional for-loop
  • Try to add each elements of List to Set using add() method of Set
  • If return value of add() method is false then it is duplicate
  • We make then print duplicate element along with its index-position

GetDuplicateFrequencyAndIndexPosition.java

package in.bench.resources.collection;

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

public class GetDuplicateFrequencyAndIndexPosition {

	public static void main(String[] args) {

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

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

		// 3. List to Set
		Set<String> set = new HashSet<String>();

		// 4. Iterate using enhanced for-each loop
		for (int index = 0; index < list.size(); index++) {

			// 5. add element to Set
			boolean flagForDuplicate = set.add(list.get(index));

			// 6 print index-position of duplicates
			if(!flagForDuplicate) {
				System.out.println("Duplciate " + list.get(index) 
				+ " at " + index + " index-position");
			}
		}
	}
}

Output:

Duplciate Kale at 3 index-position
Duplciate Garlic at 6 index-position
Duplciate Kale at 8 index-position

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Iterate through LinkedList in 5 ways
Java - How to sort Vector using Collections.sort() method ?