Removing left sided duplicates from array

Viewed 317

I'm trying to find mistakes in my code and i wonder if you could help me. My task is to write a method that would take an array as an input and return this array without left duplicates(last numbers stay). My code doesn't work in case when input is( 1,2,1,2,1,2,3 ) It returns (1, 1, 2, 3) instead of 1,2,3. Here is the code

     public static int [] solve(int [] arr){
          boolean check=false;
        ArrayList<Integer> test = new ArrayList<>(); // idk how to compete this task without ArrayList
          for (int i = 0; i < arr.length; i++) { // Here i pass my array to ArrayList
             test.add(arr[i]);
             }
             
            while(check==false) {
                for (int i = 0; i < test.size(); i++) {
                   for (int j = i + 1; j < test.size(); j++) { // Somewhere here must be my mistake that i can't find 
                       check=true;
                       if (test.get(i) == test.get(j)) {
                           test.remove(i);
                          check=false;
                       }
                   }
               }
           }
      // i created second array to return array without duplcates. 
           int arr2[];
             arr2=new int[test.size()];
             for(int i=0;i<arr2.length;i++){
                 arr2[i]=test.get(i);
             }
        
        return arr2;
    
       }
     }

I tried to complete this task on my own, so i didnt use Google to get help until now. If you know how to improve my code feel free to change everything you want to. Thank you in advance!

3 Answers

HashSet can do this since you cant put duplicate elements to HashSet, all you need to do is put your array into HashSet.

List<Integer> arr2 = new ArrayList<>(new HashSet<>(arr));

You can create a second list, iterate through your input list and for each element of your input list check if the second list contains it. If it doesn't, add it to the second list. This passes the test case that you mention in your original post:

import java.util.*;

public class MyClass {
    public static void main(String args[]) {
      List<Integer> test = new ArrayList<>();
      List<Integer> noduplicates = new ArrayList<>();
      test.add(1);
      test.add(2);
      test.add(1);
      test.add(2);
      test.add(1);
      test.add(2);
      test.add(3);
      
      for (Integer i : test) {
          if (!noduplicates.contains(i)) {
              noduplicates.add(i);
          }
      }
      System.out.println(noduplicates);
    }
}

A simple way will be to

  1. Add the numbers of the array in reverse order to a LinkedHashSet in order to keep only unique numbers as well as to preserve the last entry of each unique number.

  2. Create a List out of the LinkedHashSet.

  3. Reverse the List using Collections#reverse.

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.LinkedHashSet;
    import java.util.List;
    import java.util.Set;
    
    public class Main {
        public static void main(String[] args) {
            Set<Integer> set = new LinkedHashSet<>();
            int[] arr = { 7, 7, 4, 5, 7, 2, 7 };
    
            // Add the numbers of the array in reverse order to a LinkedHashSet in order to remove
            // duplicates as well as to preserve the last entry of each unique number.
            for (int i = arr.length - 1; i >= 0; i--) {
                set.add(arr[i]);
            }
    
            // Create a List out of the LinkedHashSet
            List<Integer> list = new ArrayList<>(set);
    
            // Reverse the List
            Collections.reverse(list);
    
            // Display the result
            System.out.println(list);
        }
    }
    

Output:

[4, 5, 2, 7]
Related