Ddnsnxe2
Ddnsnxe2

Reputation: 75

Replace all duplicates in an array

import java.util.Arrays;

public class ClassNameHere {
public static void main(String[] args) {
  

  String[] items = new String[]{"John","Bob","David", "Carrie", "John","Bob","Dawson"};
 
  String [] clone = new String[items.length];
  
   for (int i =0; i<items.length; i++) {
        clone[i] = items[i]; // copying the items array to clone
    }
  
   for (int i=items.length-1; i>=0; i--) { //trying to find duplicates.
        for (int j = i-1; j>=0; j--) {
            if (items[i].equals(items[j])) {
            
    }
  
 
  
 }
}

Hi, I am currently working on my array to remove all duplicate and change it to null value and move all null value to right side. So for example, since there are "John" and "Bob" duplicate exists, this is what I expect to make: {"David", "Carrie", "John","Bob","Dawson",null,null}.

Also, you will see I am making a clone array of items because I do not want to change any value of original items.

Upvotes: 6

Views: 593

Answers (5)

user4910279
user4910279

Reputation:

Try this.

String[] items = new String[] {"John", "Bob", "David", "Carrie", "John", "Bob", "Dawson"};
String[] clone = Arrays.copyOf(Arrays.stream(items)
    .distinct().toArray(String[]::new), items.length);
System.out.println(Arrays.toString(clone));

output:

[John, Bob, David, Carrie, Dawson, null, null]

If you don't want to use Stream

String[] items = new String[] {"John", "Bob", null, "David", "Carrie", "John", "Bob", "Dawson"};
int length = items.length;
String[] clone = new String[length];
L: for (int i = 0, last = 0; i < length; ++i) {
    String item = items[i];
    if (item == null) continue;
    for (int j = 0; j < last; ++j)
        if (item.equals(clone[j]))
            continue L;
    clone[last++] = items[i];
}
System.out.println(Arrays.toString(clone));

output:

[John, Bob, David, Carrie, Dawson, null, null, null]

Upvotes: 8

Rukshan Jayasekara
Rukshan Jayasekara

Reputation: 1985

A solution using only Arrays

Complexity - O(n^2)

 public static void main(String[] args) {
  

 String[] items = new String[]{"John","Bob","David", "Carrie", "John","Bob","Dawson"};
 
  String [] clone = new String[items.length];
  
   for (int i =0; i<items.length; i++) {
        clone[i] = items[i]; // copying the items array to clone
    }
  
   for (int i = 0; i < items.length; i++) { 
      for (int j = i + 1 ; j < items.length; j++) { 
          if (items[i].equals(items[j])) {  
              items[j] = "null"; //here we can't use null as it throws NullPointerException.
          } 
      } 
  }
  
  for (int i = 0; i < items.length; i++) {
    if (items[i].equals("null")) {
        items[i] = null;
    }
   }
    
   String[] newArray = new String[items.length];
   int index = 0;
   for (int i = 0; i < items.length; i++) {
    if (items[i] != null) {
        newArray[index++] = items[i];
    }
   }
   System.out.println(Arrays.toString(newArray)); //[John, Bob, David, Carrie, Dawson, null, null]

  }

Upvotes: 4

Rukshan Jayasekara
Rukshan Jayasekara

Reputation: 1985

Solution using HashSet

Complexity - O(n)

 public static void main(String[] args) {
  

 String[] items = new String[]{"John","Bob","David", "Carrie", "John","Bob","Dawson"};
 
  String [] clone = new String[items.length];
  
   for (int i =0; i<items.length; i++) {
        clone[i] = items[i]; // copying the items array to clone
    }
  
  HashSet set = new HashSet();
    for (int i =0; i<items.length; i++) { 
        if (set.add(items[i]) == false) { 
        items[i] = null;
    } 
        
    }
    
   //shifting the "null"s to right side.

   String[] newArray = new String[items.length];
   int index = 0;
   for (int i = 0; i < items.length; i++) {
    if (items[i] != null) {
        newArray[index++] = items[i];
    }
   }
   System.out.println(Arrays.toString(newArray)); //[John, Bob, David, Carrie, Dawson, null, null]


  }

Upvotes: 4

Niloofar Adelkhani
Niloofar Adelkhani

Reputation: 227

I hope this code help you :)

public static void main(String[] args) {

     String[] items = new String[]{"John","Bob","David", "Carrie", "John","Bob","Dawson"};

    String [] clone = new String[items.length];

    for (int i =0; i<items.length; i++) {
        clone[i] = items[i]; // copying the items array to clone
    }

    for (int i=items.length-1; i>=0; i--) { //trying to find duplicates.
        for (int j = i-1; j>=0; j--) {
            if (items[i].equals(items[j])) {
                items[i]=null;
                break;
            }
        }
        
    }
    
    int countOfNulls=0;
    for (int i=0 ; i<items.length;i++) {

        if(items[i]==null){
             countOfNulls++;
        }
    }

    for (int c=0 ;c<countOfNulls;c++) {

        for (int i=0 ; i<items.length;i++) { 

            if(items[i]==null){

                for(int j=i+1;j<items.length;j++){

                    items[j-1]=items[j];

                }

             items[items.length-1]=null;

            }

        }
    }

    System.out.println(Arrays.toString(items));
}

Upvotes: 3

shikida
shikida

Reputation: 505

You can create a 2nd array to store the elements you find at 1st array.

As long as you're walking in the 1st array, if the element you are inspecting is already present in the second array, add a null element, otherwise, copy the element into the second array.

After that, you need to exchange the null elements so they will be located in the end of the array. You can do that walking several times in the array and shifting the null value to the right, until you have no more null values on the left.

Upvotes: 2

Related Questions