I have an array elements like this:
int arr[] = {1,1,2,2,3,3,4,4};
I want to remove the duplicate elements from. Searched on the internet
public int[] removeDuplicates(int[] arr) {
int[] res = new int[arr.length];
int index = 0;
for (int num : arr) {
if (res.indexOf(num) == -1)
res[index++] = num;
}
return res;
}
This is a suboptimal solution, however it does not require any sorting of the array. I create a new array, iterate through the items in the original one and add the items to the new array if they aren't already there.
Use below method:
/*
* Method to remove duplicates from array in Java, without using
* Collection classes e.g. Set or ArrayList. Algorithm for this
* method is simple, it first sort the array and then compare adjacent
* objects, leaving out duplicates, which is already in the result.
*/
public static int[] removeDuplicates(int[] numbersWithDuplicates) {
// Sorting array to bring duplicates together
Arrays.sort(numbersWithDuplicates);
int[] result = new int[numbersWithDuplicates.length];
int previous = numbersWithDuplicates[0];
result[0] = previous;
for (int i = 1; i < numbersWithDuplicates.length; i++) {
int ch = numbersWithDuplicates[i];
if (previous != ch) {
result[i] = ch;
}
previous = ch;
}
return result;
}
public class UniqueElementinAnArray
{
public static void main(String[] args)
{
int[] a = {10,10,10,10,10,100};
int[] output = new int[a.length];
int count = 0;
int num = 0;
//Iterate over an array
for(int i=0; i<a.length; i++)
{
num=a[i];
boolean flag = check(output,num);
if(flag==false)
{
output[count]=num;
++count;
}
}
//print the all the elements from an array except zero's (0)
for (int i : output)
{
if(i!=0 )
System.out.print(i+" ");
}
}
/***
* If a next number from an array is already exists in unique array then return true else false
* @param arr Unique number array. Initially this array is an empty.
* @param num Number to be search in unique array. Whether it is duplicate or unique.
* @return true: If a number is already exists in an array else false
*/
public static boolean check(int[] arr, int num)
{
boolean flag = false;
for(int i=0;i<arr.length; i++)
{
if(arr[i]==num)
{
flag = true;
break;
}
}
return flag;
}
}
public static int[] removeDuplicates(int[] input){
int j = 0;
int i = 1;
//return if the array length is less than 2
if(input.length < 2){
return input;
}
while(i < input.length){
if(input[i] == input[j]){
i++;
}else{
input[++j] = input[i++];
}
}
int[] output = new int[j+1];
for(int k=0; k<output.length; k++){
output[k] = input[k];
}
return output;
}
Source : http://java2novice.com/java-interview-programs/remove-duplicates-sorted-array/
int flag = 0, k = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arrAns.length; j++) {
if (arr[i] == arrAns[j]) {
flag = 0;
break;
}
flag=1;
}
if (flag == 1) {
arrAns[k] = arr[i];
k++;
}
flag = 0;
}
package practice1;
import java.util.Scanner;
public class RemoveArrayDuplicatesElements {
public static void main(String[] args) {
int i, j, k, size,same = 0;
System.out.println("\nEnter array size : ");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
size = sc.nextInt();
int[] arr = new int[size+1];
System.out.println("\nAccept Numbers : ");
for (i = 0; i < size; i++)
arr[i] = sc.nextInt();
System.out.println("\nArray with Unique list : ");
for (i = 0; i < size; i++) {
for (j = i + 1; j < size;) {
if (arr[j] == arr[i]) {
same++;
for (k = j; k < size; k++) {
arr[k] = arr[k + 1];
}
size--;
} else
j++;
}
}
for (int g = 0; g < arr.length; g++) {
System.out.println(arr[g]);
}
}
}