find duplicate elements in array in java
Java
/*This method is all in one
*you can find following things:
*finding Duplicate elements in array
*array without duplicate elements
*number of duplicate elements
*numbers of pair of dulicate with repeatation
*/
//let given array = [2,3,2,5,3]
public static void findDuplicateArray(int [] array)
{
int size = array.length;
//creating array to hold count frequency of array elements
int [] countFrequency = new int[size];
// filling countFrequency with -1 value on every index
for(int i = 0; i < size; i++)
{
countFrequency[i] = -1;//[-1,-1,-1,-1,-1...]
}
int count = 1;
for(int i = 0; i < size; i++)
{
//check countFrequency[i] != 0 because 0 means it already counted
if(countFrequency[i] != 0)
{
for(int j = i+1; j < size; j++)
{
//if array[i] == array[j] then increase count value
if(array[i] == array[j])
{
count++;
/*only at first occurence of an element count value
*will be increased else everywhere it will be 0
*/
countFrequency[j]= 0;
}
}
countFrequency[i] = count;
}
count = 1;
}
// array = [2,3,2,5,3]
//countFrequency = [2,2,0,1,0]
System.out.println("array without duplicate elements");
for(int i = 0; i < array.length; i++)
{
if(countFrequency[i] >= 1)
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println("duplicate elements in array");
for(int i = 0; i < array.length; i++)
{
if(countFrequency[i]/2 >= 1)
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println("number of duplicate elements");
count = 0;
for(int i = 0; i < array.length; i++)
{
if(countFrequency[i]/2 >= 1)
count++;
}
System.out.print(count);
System.out.println();
System.out.println("numbers of pair of dulicate with repeatation");
count = 0;
for(int i = 0; i < array.length; i++)
{
if(countFrequency[i] >= 2)
{
int div = countFrequency[i]/2;
count+=div;
}
}
System.out.println(count);
int [] array3 = new int [array.length];
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < countFrequency[i]; j++)
{
array3[i]= array[i];
}
}
}
Also in Java:
- Title
- java boolean zen
- Category
- Java
- Title
- java concatenate strings
- Category
- Java
- Title
- Compilation is not supported for following modules: android. Unfortunately you can't have non-Gradle Java modules and Android-Gradle modules in one project
- Category
- Java
- Title
- take string from keyboard java
- Category
- Java
- Title
- how to do 4th root java
- Category
- Java
- Title
- using java 8 stream to process data in java
- Category
- Java
- Title
- java string format with placeholder
- Category
- Java
- Title
- how to get array input in java
- Category
- Java
- Title
- float to string java
- Category
- Java
- Title
- processing library cassette
- Category
- Java
- Title
- inputstream to string java
- Category
- Java
- Title
- java hash password
- Category
- Java
- Title
- java read string input
- Category
- Java
- Title
- parseints(str) java
- Category
- Java
- Title
- arraylist add method
- Category
- Java
- Title
- Java array multidimensional hashcode
- Category
- Java
- Title
- How to make a class in Java?
- Category
- Java
- Title
- how to remove a certain string in a arraylist java
- Category
- Java
- Title
- how to have a only number type in java
- Category
- Java
- Title
- java getter
- Category
- Java
- Title
- java replace all xml decoding
- Category
- Java
- Title
- JavaFX mouse over effe
- Category
- Java
- Title
- what is enum in java
- Category
- Java
- Title
- how to end a program in an if statement java
- Category
- Java
- Title
- how to count an replace string in java
- Category
- Java
- Title
- android get distance between two locations kotlin
- Category
- Java
- Title
- enumerated data types in java
- Category
- Java
- Title
- Building a maven EAR project and specifying the configuration of which projects to include, what is the element in the plugin configuration that contains Enterprise Java Bean Projects:
- Category
- Java
- Title
- java map foreach
- Category
- Java
- Title
- processing audio android
- Category
- Java
- Title
- java program to get all cat indices in elasticsearch javadoc
- Category
- Java
- Title
- find length of array java
- Category
- Java
- Title
- Java nested array hashcode
- Category
- Java
- Title
- imageview.setbackground
- Category
- Java
- Title
- how to close a jframe in java with an if statement
- Category
- Java
- Title
- reverse number in java
- Category
- Java
- Title
- add element to stack java
- Category
- Java
- Title
- java script zip function
- Category
- Java
- Title
- validation list empty java
- Category
- Java
- Title
- java define a generic class that produces
- Category
- Java
- Title
- Android recycleView
- Category
- Java
- Title
- eclipse does not support logback with spring boot
- Category
- Java
- Title
- java stream sort
- Category
- Java
- Title
- spigot custom join message
- Category
- Java
- Title
- java last element in array
- Category
- Java
- Title
- java read lines from file
- Category
- Java
- Title
- java append to array
- Category
- Java
- Title
- java stream order by property
- Category
- Java
- Title
- big integer java
- Category
- Java
- Title
- java for increment by 2
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- open an existing excel file in java apache poi
- Category
- Java
- Title
- java convert string with commas to long
- Category
- Java
- Title
- java stack push
- Category
- Java
- Title
- how to create a hashmap in java
- Category
- Java
- Title
- Please ensure that your JAVA_HOME points to a valid Java SDK. You are currently pointing to: /usr/lib/jvm/java-13-oracle
- Category
- Java
- Title
- string array in java methods
- Category
- Java
- Title
- use regex in if statement java
- Category
- Java
- Title
- java convert a string to char[]
- Category
- Java
- Title
- kotlin add value to MutableLiveData
- Category
- Java
- Title
- java swing timer sleep
- Category
- Java
- Title
- spigot respawn player location
- Category
- Java
- Title
- How to chage font progrmatically
- Category
- Java
- Title
- java exceptions for catch
- Category
- Java
- Title
- how to play an audio in java
- Category
- Java
- Title
- how to compare current date and time with another date and time in android
- Category
- Java
- Title
- serialversionuid java
- Category
- Java
- Title
- number to char java
- Category
- Java
- Title
- bukkit register commands
- Category
- Java
- Title
- how to input in java
- Category
- Java
- Title
- java square a number
- Category
- Java
- Title
- java delay
- Category
- Java
- Title
- java for each loop
- Category
- Java
- Title
- traversal of list in java
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- string contains java
- Category
- Java
- Title
- keep jframe on top
- Category
- Java
- Title
- remove item from arraylist in java
- Category
- Java
- Title
- read and write files pattern java
- Category
- Java
- Title
- javafx textarea how to make smaller
- Category
- Java
- Title
- java arraylist in enum
- Category
- Java
- Title
- spigot repeating task
- Category
- Java
- Title
- java import text file into arraylist
- Category
- Java
- Title
- how to add strings in java
- Category
- Java
- Title
- junit meaning in java
- Category
- Java
- Title
- print in one line in java
- Category
- Java
- Title
- how to compare strings java
- Category
- Java
- Title
- java vs javascript
- Category
- Java
- Title
- substring java
- Category
- Java
- Title
- how to pass a float between activities in android studio
- Category
- Java
- Title
- java hashmap put
- Category
- Java
- Title
- check java version cmd
- Category
- Java
- Title
- java how to call getReader twice
- Category
- Java
- Title
- java double 2 decimal
- Category
- Java
- Title
- StringBuilder java pop back
- Category
- Java
- Title
- how to detect device javascirpt
- Category
- Java
- Title
- loop while in java
- Category
- Java
- Title
- java insert into arraylist
- Category
- Java
- Title
- create object of static class in java
- Category
- Java
- Title
- convert string to mayus java
- Category
- Java