how to remove duplicates from an array java
Java
// Must sort arrays first --> Arrays.sort(arrayName)
public class RemoveDuplicateInArrayExample{
public static int removeDuplicateElements(int arr[], int n){
if (n==0 || n==1){
return n;
}
int[] temp = new int[n];
int j = 0;
for (int i=0; i<n-1; i++){
if (arr[i] != arr[i+1]){
temp[j++] = arr[i];
}
}
temp[j++] = arr[n-1];
// Changing original array
for (int i=0; i<j; i++){
arr[i] = temp[i];
}
return j;
}
public static void main (String[] args) {
int arr[] = {10,20,20,30,30,40,50,50};
int length = arr.length;
length = removeDuplicateElements(arr, length);
//printing array elements
for (int i=0; i<length; i++)
System.out.print(arr[i]+" ");
}
} remove duplicate element in array - java
public class RemoveDuplicateInArrayExample{
public static int removeDuplicateElements(int arr[], int n){
if (n==0 || n==1){
return n;
}
int[] temp = new int[n];
int j = 0;
for (int i=0; i<n-1; i++){
if (arr[i] != arr[i+1]){
temp[j++] = arr[i];
}
}
temp[j++] = arr[n-1];
// Changing original array
for (int i=0; i<j; i++){
arr[i] = temp[i];
}
return j;
}
public static void main (String[] args) {
int arr[] = {10,20,20,30,30,40,50,50};
int length = arr.length;
length = removeDuplicateElements(arr, length);
//printing array elements
for (int i=0; i<length; i++)
System.out.print(arr[i]+" ");
}
}
Also in Java:
- Title
- java int to binary string
- Category
- Java
- Title
- reverse number in java
- Category
- Java
- Title
- how to calculate exponential in java
- Category
- Java
- Title
- change number into array in java
- Category
- Java
- Title
- java random seed
- Category
- Java
- Title
- cordova cannot find symbol import com.google.firebase.iid.FirebaseMessagingService;
- Category
- Java
- Title
- remove part of string java
- Category
- Java
- Title
- sort array java
- Category
- Java
- Title
- java math.random
- Category
- Java
- Title
- arraylist of double
- Category
- Java
- Title
- java string array to arraylist
- Category
- Java
- Title
- java check if int
- Category
- Java
- Title
- entre clavier java
- Category
- Java
- Title
- java check if string is number
- Category
- Java
- Title
- java replace all html tags
- Category
- Java
- Title
- split every character in string into array java
- Category
- Java
- Title
- java parse json
- Category
- Java
- Title
- how to init an array with objects in java
- Category
- Java
- Title
- how to detect if someone clicks on a jpanel in java
- Category
- Java
- Title
- read a file in java and store as integer array using buffered reader
- Category
- Java
- Title
- 2d arrays | java
- Category
- Java
- Title
- java generate list of random element
- Category
- Java
- Title
- countdown timer with seekbar
- Category
- Java
- Title
- java program to display characters from a to z using loop
- Category
- Java
- Title
- java switch multiple values
- Category
- Java
- Title
- java how to print a string[]
- Category
- Java
- Title
- number to string java
- Category
- Java
- Title
- how to print a 2d array in java
- Category
- Java
- Title
- how to remove all whitespace from string java
- Category
- Java
- Title
- java while
- Category
- Java
- Title
- java set example
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- java array swap
- Category
- Java
- Title
- array in line java
- Category
- Java
- Title
- jbutton remove selection border
- Category
- Java
- Title
- java letter alphabet index
- Category
- Java
- Title
- string method example in java
- Category
- Java
- Title
- how to read file from assets folder in android
- Category
- Java
- Title
- bufferedwriter doesn't write to file
- Category
- Java
- Title
- place.getlatlng() returning null
- Category
- Java
- Title
- print a letter in java
- Category
- Java
- Title
- java random number between 100 and 999
- Category
- Java
- Title
- how to check if a list is empty java
- Category
- Java
- Title
- java make arraylist
- Category
- Java
- Title
- java for each
- Category
- Java
- Title
- processing library cassette
- Category
- Java
- Title
- java array declaration
- Category
- Java
- Title
- java list contains object with property
- Category
- Java
- Title
- non primitive data types in java
- Category
- Java
- Title
- changing double space to single in java script
- Category
- Java
- Title
- how to add jar in maven java application in netbeans
- Category
- Java
- Title
- index of an array procesing
- Category
- Java
- Title
- java variable string
- Category
- Java
- Title
- how to change top of window in java
- Category
- Java
- Title
- how to pass parameters to xsl file
- Category
- Java
- Title
- annotation spring notnull
- Category
- Java
- Title
- j
- Category
- Java
- Title
- java execute for cycle parallel thread
- Category
- Java
- Title
- android how to split string
- Category
- Java
- Title
- how to convert char to uppercase java
- Category
- Java
- Title
- java startActivity crash
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- math minimum java
- Category
- Java
- Title
- java code to get all leaf nodes of a xml
- Category
- Java
- Title
- java optional to collection
- Category
- Java
- Title
- summary of operator java
- Category
- Java
- Title
- equls en java
- Category
- Java
- Title
- java manacher's algorithm
- Category
- Java
- Title
- count word in string no matter the delimiter java
- Category
- Java
- Title
- java console text color
- Category
- Java
- Title
- android java retrofit offline cache
- Category
- Java
- Title
- java pause program until key pressed
- Category
- Java
- Title
- java set value of arraylist
- Category
- Java
- Title
- could not initialize defaultkotlinsourcesetkt intelij
- Category
- Java
- Title
- enum in java
- Category
- Java
- Title
- how to use the this keyword in java
- Category
- Java
- Title
- Category
- Java
- Title
- java script print date in YYYY-MM-DD HH:MM:SS format
- Category
- Java
- Title
- how to find substring in java
- Category
- Java
- Title
- how to make a pre set list java
- Category
- Java
- Title
- java classes and methods
- Category
- Java
- Title
- how to create gravity in java
- Category
- Java
- Title
- remove spaces at beginning and end of string java
- Category
- Java
- Title
- java how to read file extension
- Category
- Java
- Title
- mutable string in java
- Category
- Java
- Title
- java 8 validate based on pair of strings
- Category
- Java
- Title
- bukkit java set leather armor color from hex
- Category
- Java
- Title
- print int in java
- Category
- Java
- Title
- how to create array of linkedlist in java
- Category
- Java
- Title
- java script dynamic varibale creation
- Category
- Java
- Title
- how to add an item to a list in python
- Category
- Java
- Title
- write file from a specific location in java
- Category
- Java
- Title
- java open file
- Category
- Java
- Title
- java script print date in YYYY-MM-DD format
- Category
- Java
- Title
- spigot heal player
- Category
- Java
- Title
- print arraylist java
- Category
- Java
- Title
- programa que convierete un archi de c a java
- Category
- Java
- Title
- show menuitem when menu hovered javafx
- Category
- Java
- Title
- jsonobject to java object
- Category
- Java
- Title
- inheritance in java
- Category
- Java