In the previous article, we have seen Java Program to Remove Duplicate Elements in an Array
In this article we are going to find duplicates elements in a string array in Java.
Java Program to Find the Duplicate Values of an Array of String Values
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to find duplicate string value in a string array.
Method-1: Java Program to Find the Duplicate Values of an Array of String Values By Static Initialization of Array Elements
Approach:
- Create a string array.
- Display the array.
- Traverse through the array and print all duplicate elements from the array by comparing them to the next element.
Program:
import java.util.*; public class Main { public static void main(String args[]) { // Crating an array String arr[] = {"cat", "dog", "mouse", "elephant", "cat", "mouse", "lion", "zebra"}; // Displaying the array System.out.print("Array: "); printArray(arr); System.out.print("The duplicate elements are : "); // Print non duplicate elements printDupes(arr); } // Function to print the array static void printArray(String arr[]) { for(int i = 0; i < arr.length ; i++) System.out.print(arr[i]+" "); System.out.println(); } public static void printDupes(String arr[]) { // Counter int j = 0; // Checks for duplicate elements for (int i=0; i<arr.length-1; i++) { for(j=i+1;j<arr.length;j++) if (arr[i].equals(arr[j])&&i!=j) { System.out.print(arr[j]+" "); } } } }
Output: Array: cat dog mouse elephant cat mouse lion zebra The duplicate elements are : cat mouse
Method-2: Java Program to Find the Duplicate Values of an Array of String Values By Dynamic Initialization of Array Elements
Approach:
- Take input of array size.
- Then take input of string array elements.
- Display the array.
- Traverse through the array and print all duplicate elements from the array by comparing them to the next element.
Program:
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of elements in the array: "); int num = sc.nextInt(); String arr[] = new String[num]; System.out.println("Enter the elements: "); for (int i = 0; i <arr.length; i++) { arr[i] = sc.next(); } // Displaying the array System.out.print("Array: "); printArray(arr); System.out.print("The duplicate elements are : "); // Print non duplicate elements printDupes(arr); } // Function to print the array static void printArray(String arr[]) { for(int i = 0; i < arr.length ; i++) System.out.print(arr[i]+" "); System.out.println(); } public static void printDupes(String arr[]) { // Counter int j = 0; // Checks for duplicate elements for (int i=0; i<arr.length-1; i++) { for(j=i+1;j<arr.length;j++) if (arr[i].equals(arr[j])&&i!=j) { System.out.print(arr[j]+" "); } } } }
Output: Enter the number of elements in the array: 5 Enter the elements: rat cat bat rat mat Array: rat cat bat rat mat The duplicate elements are : rat
Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Related Java Programs: