There are multiple ways to find the duplicate elements in java.
I am giving examples of finding the duplicate elements in java.
Best way to find the duplicate elements in java is to use the sort the elements of the array and compare each element with their next element.
I have commented some of the method, if you remove the comment before the function call then you can check one by one. I found this is very useful in real time scenario and solving the interview question.
Using this sorting technique we can perform String manipulation as well, because String is nothing but it's also built up with array. You can be able to check the duplicate character in string as well.
Example:
package com.vikash.java8;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class ArrayDuplicate {
public static void main(String args[]) {
int array[] = { 5, 5, 3, 8, 2, 1, 8, 3, 3, 1 };
ArrayDuplicate dup = new ArrayDuplicate();
// dup.myFunction(array);
System.out.println();
// dup.withoutSorting(array);
// dup.bubbleSort(array);
dup.findDuplicateUsingMap(array);
}
public void myFunction(int array[]) {
Arrays.sort(array);
System.out.println("After Sorting the elements");
for (int a : array) {
System.out.print(a + " ");
}
System.out.println();
for (int i = 0; i < array.length - 1; i++) {
if (array[i] == array[i + 1]) {
System.out.println("Duplicate found::" + array[i] + ":::" + array[i + 1]);
}
}
}
public void withoutSorting(int[] array) {
for (int i = 1; i < array.length; i++) {
for (int j = 0; j < i; j++) {
if (array[j] == array[i]) {
System.out.println("Duplicate Found::" + array[j] + "::" + array[i]);
}
}
}
}
public void bubbleSort(int[] array) {
int temp;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
for (int e = 0; e < array.length - 1; e++) {
if (array[e] == array[e + 1]) {
System.out.println("Duplicate Found::" + array[e]);
}
}
}
public void findDuplicateUsingMap(int[] array) {
Map<Integer, Integer> myMap = new HashMap();
for (int i = 0; i < array.length; i++) {
if (myMap.containsKey(i)) {
myMap.put(array[i], myMap.get(i) + 1);
} else {
myMap.put(array[i], 1);
}
}
for (Map.Entry<Integer, Integer> itr : myMap.entrySet()) {
System.out.println(itr.getKey() + " " + itr.getValue());
}
}
}
I am giving examples of finding the duplicate elements in java.
Best way to find the duplicate elements in java is to use the sort the elements of the array and compare each element with their next element.
I have commented some of the method, if you remove the comment before the function call then you can check one by one. I found this is very useful in real time scenario and solving the interview question.
Using this sorting technique we can perform String manipulation as well, because String is nothing but it's also built up with array. You can be able to check the duplicate character in string as well.
Example:
package com.vikash.java8;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class ArrayDuplicate {
public static void main(String args[]) {
int array[] = { 5, 5, 3, 8, 2, 1, 8, 3, 3, 1 };
ArrayDuplicate dup = new ArrayDuplicate();
// dup.myFunction(array);
System.out.println();
// dup.withoutSorting(array);
// dup.bubbleSort(array);
dup.findDuplicateUsingMap(array);
}
public void myFunction(int array[]) {
Arrays.sort(array);
System.out.println("After Sorting the elements");
for (int a : array) {
System.out.print(a + " ");
}
System.out.println();
for (int i = 0; i < array.length - 1; i++) {
if (array[i] == array[i + 1]) {
System.out.println("Duplicate found::" + array[i] + ":::" + array[i + 1]);
}
}
}
public void withoutSorting(int[] array) {
for (int i = 1; i < array.length; i++) {
for (int j = 0; j < i; j++) {
if (array[j] == array[i]) {
System.out.println("Duplicate Found::" + array[j] + "::" + array[i]);
}
}
}
}
public void bubbleSort(int[] array) {
int temp;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
for (int e = 0; e < array.length - 1; e++) {
if (array[e] == array[e + 1]) {
System.out.println("Duplicate Found::" + array[e]);
}
}
}
public void findDuplicateUsingMap(int[] array) {
Map<Integer, Integer> myMap = new HashMap();
for (int i = 0; i < array.length; i++) {
if (myMap.containsKey(i)) {
myMap.put(array[i], myMap.get(i) + 1);
} else {
myMap.put(array[i], 1);
}
}
for (Map.Entry<Integer, Integer> itr : myMap.entrySet()) {
System.out.println(itr.getKey() + " " + itr.getValue());
}
}
}