java find duplicates in array
Java
package dto;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* Java Program to find duplicate elements in an array. There are two straight
* forward solution of this problem first, brute force way and second by using
* HashSet data structure. A third solution, similar to second one is by using
* hash table data structure e.g. HashMap to store count of each element and
* print element with count 1.
*
* @author java67
*/
public class DuplicatesInArray{
public static void main(String args[]) {
String[] names = { "Java", "JavaScript", "Python", "C", "Ruby", "Java" };
// First solution : finding duplicates using brute force method
System.out.println("Finding duplicate elements in array using brute force method");
for (int i = 0; i < names.length; i++) {
for (int j = i + 1; j < names.length; j++) {
if (names[i].equals(names[j]) ) {
// got the duplicate element
}
}
}
// Second solution : use HashSet data structure to find duplicates
System.out.println("Duplicate elements from array using HashSet data structure");
Set<String> store = new HashSet<>();
for (String name : names) {
if (store.add(name) == false) {
System.out.println("found a duplicate element in array : "
+ name);
}
}
// Third solution : using Hash table data structure to find duplicates
System.out.println("Duplicate elements from array using hash table");
Map<String, Integer> nameAndCount = new HashMap<>();
// build hash table with count
for (String name : names) {
Integer count = nameAndCount.get(name);
if (count == null) {
nameAndCount.put(name, 1);
} else {
nameAndCount.put(name, ++count);
}
}
// Print duplicate elements from array in Java
Set<Entry<String, Integer>> entrySet = nameAndCount.entrySet();
for (Entry<String, Integer> entry : entrySet) {
if (entry.getValue() > 1) {
System.out.println("Duplicate element from array : "
+ entry.getKey());
}
}
}
}
Output :
Finding duplicate elements in array using brute force method
Duplicate elements from array using HashSet data structure
found a duplicate element in array : Java
Duplicate elements from array using hash table
Duplicate element from array : Java// Uses a set, which does not allow duplicates
for (String name : names) {
if (set.add(name) == false) {
// your duplicate element
}
}
Also in Java:
- Title
- full shutdown windows 10 cmd
- Category
- Java
- Title
- fragment manager in android
- Category
- Java
- Title
- joptionpane fonctionnement java
- Category
- Java
- Title
- spigot cancel repeating task
- Category
- Java
- Title
- java how to change the length of an array
- Category
- Java
- Title
- java instantiate collection with values
- Category
- Java
- Title
- logger in java
- Category
- Java
- Title
- get drawable with string android java
- Category
- Java
- Title
- how to stop screen rotation in android code
- Category
- Java
- Title
- how to change top of window in java
- Category
- Java
- Title
- iterate map in java 8 using stream
- Category
- Java
- Title
- join two lists java
- Category
- Java
- Title
- how to check if an arraylist contains a value in java recursion
- Category
- Java
- Title
- how to do 4th root java
- Category
- Java
- Title
- how to count the number of occurrences of an element in a arraylist in java
- Category
- Java
- Title
- how to check wether the property exist in a object in java script
- Category
- Java
- Title
- java if
- Category
- Java
- Title
- string replace java
- Category
- Java
- Title
- write file from a specific location in java
- Category
- Java
- Title
- java string to uuid
- Category
- Java
- Title
- android create snackbar
- Category
- Java
- Title
- java read from file
- Category
- Java
- Title
- java split into arraylist
- Category
- Java
- Title
- how to iterate hashmap java
- Category
- Java
- Title
- string to arraylist convert java
- Category
- Java
- Title
- how to open a folder in java swing project
- Category
- Java
- Title
- how to read to into a file in java
- Category
- Java
- Title
- java script dynamic varibale creation
- Category
- Java
- Title
- java constructor genertic
- Category
- Java
- Title
- java public static void main
- Category
- Java
- Title
- simple javascript to detect browser using java utils
- Category
- Java
- Title
- check if map contains key java
- Category
- Java
- Title
- reading in lines from a file java
- Category
- Java
- Title
- how to check if rs next is null
- Category
- Java
- Title
- java while
- Category
- Java
- Title
- best companies to workk in
- Category
- Java
- Title
- store string elements in character array in java
- Category
- Java
- Title
- kotlin add element to array
- Category
- Java
- Title
- java foreach map
- Category
- Java
- Title
- java
- Category
- Java
- Title
- java nested loop
- Category
- Java
- Title
- reverse linked list in java to get both head and tail
- Category
- Java
- Title
- java swing dialog box
- Category
- Java
- Title
- java array to list
- Category
- Java
- Title
- fibonacci sequence java
- Category
- Java
- Title
- java execute jar from main
- Category
- Java
- Title
- creating file in specific location in java application
- Category
- Java
- Title
- java script print date in YYYY-MM-DD HH:MM:SS format
- Category
- Java
- Title
- how to declare array java
- Category
- Java
- Title
- create instance object java
- Category
- Java
- Title
- how to check if in array java
- Category
- Java
- Title
- Java.awt graphics tutorial
- Category
- Java
- Title
- non primitive data types in java
- Category
- Java
- Title
- what is a variable in java
- Category
- Java
- Title
- string to char in java
- Category
- Java
- Title
- java code to get all leaf nodes of a xml
- Category
- Java
- Title
- set color of text for jlabel
- Category
- Java
- Title
- how to find complement of a number in java
- Category
- Java
- Title
- java split for multiple characters
- Category
- Java
- Title
- import
- Category
- Java
- Title
- web app runtime for java
- Category
- Java
- Title
- binary to octal conversion java program
- Category
- Java
- Title
- java string replace character at position
- Category
- Java
- Title
- how to initialize main in java
- Category
- Java
- Title
- iterator loop java
- Category
- Java
- Title
- run java program maven
- Category
- Java
- Title
- connecting to h2 database from java
- Category
- Java
- Title
- how to open a text file in java
- Category
- Java
- Title
- convert string into unicode java
- Category
- Java
- Title
- java enum
- Category
- Java
- Title
- arraylist array
- Category
- Java
- Title
- java define a generic class that produces
- Category
- Java
- Title
- spigot spawn firework
- Category
- Java
- Title
- import java.io.serializable
- Category
- Java
- Title
- phoneword converter
- Category
- Java
- Title
- print in one line in java
- Category
- Java
- Title
- java how to show an array
- Category
- Java
- Title
- java foreach
- Category
- Java
- Title
- how to create an action listener in java
- Category
- Java
- Title
- set view layout params android
- Category
- Java
- Title
- read and write files pattern java
- Category
- Java
- Title
- java if a or b
- Category
- Java
- Title
- java infinitew recursion
- Category
- Java
- Title
- java script print date in YYYY-MM-DD format
- Category
- Java
- Title
- find frequency of each word in a string in java
- Category
- Java
- Title
- java substring
- Category
- Java
- Title
- java read from connection even if 404
- Category
- Java
- Title
- how to print ascii value in java
- Category
- Java
- Title
- break for loop java
- Category
- Java
- Title
- what is a float java
- Category
- Java
- Title
- generic method lambda java
- Category
- Java
- Title
- reverse array in java
- Category
- Java
- Title
- loop while in java
- Category
- Java
- Title
- error attribute fabattached not found
- Category
- Java
- Title
- bubble sort in java
- Category
- Java
- Title
- taking string input in java
- Category
- Java
- Title
- do statement java
- Category
- Java
- Title
- java delay
- Category
- Java
- Title
- linux command to see all the java version installed
- Category
- Java
- Title
- parsedouble java
- Category
- Java