I having a problem with coding this:
Write a static method named removeDuplicates
that takes as input an array of integers and returns as a result a new
hey all you can use this code that i create!!!
import java.util.*;
public class DistinctNumber {
public static void main(String[] args) {
int[] nums= {1,3,2,3,4,3,2,5,4,6};
int [] T2 = duplicate(nums);
for (int i = 0; i < T2.length; i++) {
System.out.println(T2[i]);
}
}
public static boolean exist(int x,int []A){
for (int i = 0; i < A.length; i++) {
if(x==A[i]){
return true;
}
}
return false;
}
public static int [] EliminateDuplicate(int [] numbers){
int [] B = new int[numbers.length];
int i=0,j=0;
for(i=0;i<numbers.length;i++){
if(!exist(numbers[i], B)){
B[j] = numbers[i];
j++;
}
}
int[] C = new int[j];
for (int k = 0; k < C.length; k++) {
C[k] = B[k];
}
return C;
}
}
public class Foo {
public static void main(String[] args) {
//example input
int input[] = new int[]{1, 6 , 5896, 5896, 9, 100,7, 1000, 8, 9, 0, 10, 90, 4};
//use list because the size is dynamical can change
List<Integer> result = new ArrayList<Integer>();
for(int i=0; i<input.length; i++)
{
boolean match = false;
for(int j=0; j<result.size(); j++)
{
//if the list contains any input element make match true
if(result.get(j) == input[i])
match = true;
}
//if there is no matching we can add the element to the result list
if(!match)
result.add(input[i]);
}
// Print the result
for(int i=0; i<result.size(); i++)
System.out.print(result.get(i) + ", ");
}
} output: 1, 6, 5896, 9, 100, 7, 1000, 8, 0, 10, 90, 4,
First of all, you should know length without duplicates(dups): initial length minus number of dups. Then create new array with right length. Then check each element of list[] for dups, if dup founded - check next element, if dup not founded - copy element to new array.
public static int[] eliminateDuplicates(int[] list) {
int newLength = list.length;
// find length w/o duplicates:
for (int i = 1; i < list.length; i++) {
for (int j = 0; j < i; j++) {
if (list[i] == list[j]) { // if duplicate founded then decrease length by 1
newLength--;
break;
}
}
}
int[] newArray = new int[newLength]; // create new array with new length
newArray[0] = list[0]; // 1st element goes to new array
int inx = 1; // index for 2nd element of new array
boolean isDuplicate;
for (int i = 1; i < list.length; i++) {
isDuplicate = false;
for (int j = 0; j < i; j++) {
if (list[i] == list[j]) { // if duplicate founded then change boolean variable and break
isDuplicate = true;
break;
}
}
if (!isDuplicate) { // if it's not duplicate then put it to new array
newArray[inx] = list[i];
inx++;
}
}
return newArray;
}
public class Test
static int[] array = {4, 3, 3, 4, 5, 2, 4};
static HashSet list = new HashSet();
public static void main(String ar[])
{
for(int i=0;i<array.length;i++)
{
list.add(array[i]);
}
System.out.println(list);
}}
The Output is : [2, 3, 4, 5]
You can use HashSet that does not allow dulplicate elements
public static void deleteDups(int a []) {
HashSet<Integer> numbers = new HashSet<Integer>();
for(int n : a)
{
numbers.add(n);
}
for(int k : numbers)
{
System.out.println(k);
}
System.out.println(numbers);
}
public static void main(String[] args) {
int a[]={2,3,3,4,4,5,6};
RemoveDuplicate.deleteDups(a);
}
}
o/p is 2
3
4
5
6
[2, 3, 4, 5, 6]
To Preserve the ordering and to remove duplicates in the integer array, you can try this:
public void removeDupInIntArray(int[] ints){
Set<Integer> setString = new LinkedHashSet<Integer>();
for(int i=0;i<ints.length;i++){
setString.add(ints[i]);
}
System.out.println(setString);
}
Hope this helps.