21

I have an Array from which i want to remove Duplicate items.

for(int data1=startpos;data1<=lastrow;data1++) {
    String movie_soundtrk=cells.getCell(data1,Mmovie_sndtrk_cl).getValue().toString();
    al.add(movie_soundtrk);
}

String commaSeparated=al.toString();
String [] items = commaSeparated.split(",");
String[] trimmedArray = new String[items.length];
for (int i = 0; i < items.length; i++) {
    trimmedArray[i] = items[i].trim();
}

Set<String> set = new HashSet<String>();
Collections.addAll(set, trimmedArray);

System.out.println(set);

But this is not giving me unique Values from Array.

My Array:- {English, French, Japanese, Russian, Chinese Subtitles,English, French, Japanese, Russian, Chinese Subtitles}

Out Put :- [Japanese, Russian, French, Chinese Subtitles], Chinese Subtitles, [English, English]

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
Code Hungry
  • 3,930
  • 22
  • 67
  • 95

13 Answers13

62

You can do it in one line in java 7:

String[] unique = new HashSet<String>(Arrays.asList(array)).toArray(new String[0]);

and shorter and simpler in java 8:

String[] unique = Arrays.stream(array).distinct().toArray(String[]::new);
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • This does not work if the type is a primitive array, e.g., if "array" in the above would be a byte[][] array (and the result of Arrays.asList would be a List), and I am not sure how to use Stream.distinct in this cases. – Alex Feb 03 '16 at 14:21
  • @alex you can't use this or anything from the JDK to find unique arrays, because javs arrays are not `Comparable`; they are all unique, even if their contents are identical. You'll have to implement your own code to do the work, or use `List>` instead of `byte[][]`, because Lists *are* `Comparable` (they compare their elements). – Bohemian Feb 03 '16 at 20:10
  • 1
    `Comparable` does not matter for `distinct()`, as it checks for identical objects using the `equals()` method. Excerpt from the docs: `Returns a stream consisting of the distinct elements (according to Object.equals(Object)) of this stream. ` – Frederic Leitenberger Sep 27 '16 at 17:54
  • I would like to know what is time complexity for this solution. Could you please explain? – Liu Bei Aug 03 '21 at 09:32
  • @Liu the time complexity is O(n), where n is the String length, because all operations on a HashSet (which backs Stream's distinct()) are constant time (ie O(1)) and there are n operations - 1 for each character. – Bohemian Aug 03 '21 at 09:36
3

HashSet will do the job.

You can try this:

List<String> newList = new ArrayList<String>(new HashSet<String>(oldList));
flk
  • 171
  • 1
  • 3
3

If you don't wanna use Hashset or the new method in Java8 mentioned above you can write this code you just need first to sort the array so similar values will then be next to each other and then count the number of distinct pairs in adjacent cells.

    public static int solution(int[] A) {
    int count = 1;
    Arrays.sort(A);
    for (int i = 1; i < A.length - 1; i++) {
        if (A[i] != A[i + 1]) {
            count++;
        }
    }
    return count;
}
Mustafa Shahoud
  • 640
  • 6
  • 13
2

Using the Stream API of Java 8 this is a solution with a generic Array type:

public static <T> T[] makeUnique(T... values)
{
    return Arrays.stream(values).distinct().toArray(new IntFunction<T[]>()
    {

        @Override
        public T[] apply(int length)
        {
            return (T[]) Array.newInstance(values.getClass().getComponentType(), length);
        }

    });
}

It works for any Object type array, but not for primitive arrays.

For primitive arrays it looks like this:

public static int[] makeUnique(int... values)
{
    return Arrays.stream(values).distinct().toArray();
}

And finally here is a little unit test:

@Test
public void testMakeUnique()
{
    assertArrayEquals(new String[] { "a", "b", "c" }, makeUnique("a", "b", "c", "b", "a"));
    assertArrayEquals(new Object[] { "a", "b", "c" }, makeUnique(new Object[] { "a", "b", "c", "b", "a" }));
    assertArrayEquals(new Integer[] { 1, 2, 3, 4, 5 }, makeUnique(new Integer[] { 1, 2, 2, 3, 3, 3, 1, 4, 5, 5, 5, 1 }));
    assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, makeUnique(new int[] { 1, 2, 2, 3, 3, 3, 1, 4, 5, 5, 5, 1 }));
}
Frederic Leitenberger
  • 1,949
  • 24
  • 32
1

If your array satisfied these two conditions -:

  1. Only duplicates and single value allowed (No triplicate or greater allowed)

  2. There should only be one unique value in the array

     // Use Bitwise 'exclusive or' operator to find unique value 
     int result = array[0];
     for (int i = 1; i < array.length; i++) {
         result  ^= array[i];
     }
     System.out.println(result);
    

    } }

0

You could get two sets, one with all the subtitles, and the other with the duplicates

String[] trimmedArray = new String[items.length];
Set<String> subtitles = new HashSet<String>();
Set<String> duplicatedSubtitles = new HashSet<String>();

foreach(String subtitle : trimmedArray){
    subtitle = subtitle.trim();
    if(subtitles.contains(subtitle)){
        duplicatedSubtitles.add(subtitle);
    }
    subtitles.add(subtitle);
}
Oscar Castiblanco
  • 1,626
  • 14
  • 31
0

Try instead of this

Set<String> set = new HashSet<String>();

to call this

set.addAll(trimmedArray);
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
0

Why did you first add items into array and then convert it to string? Just iterate over tha array and copy them to Set.Then print new created set which holds unique values.

Set<String> set = new HashSet<String>();
for (int i = 0; i < al.length; i++) {
    set.add(al[i]);
}

for (String str : set) {
    System.out.println(str);
}
Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
  • This is Giving me Out put as Japanese Russian French Chinese Subtitles] Chinese Subtitles [English English It should be only Japanese Russian French Chinese Subtitles English – Code Hungry Dec 10 '12 at 08:25
  • If your array contains the values you write, then it this code works as expected.I think there is a problem with your array. Check your array again.Be sure that it holds string values. – Parvin Gasimzade Dec 10 '12 at 08:30
0

This code will calculates distinct elements from an array, then finds their occurrence. And calculates percentage and save it to hashmap.

int _occurrence = 0;
        String[] _fruits = new String[] {"apple","apple","banana","mango","orange","orange","mango","mango","banana","banana","banana","banana","banana"};
        List<String> _initialList = Arrays.asList(_fruits);
        Set<String> treesetList = new TreeSet<String>(_initialList);
        String[] _distinct =  (String[]) treesetList.toArray(new String[0]);

        HashMap<String,String> _map = new HashMap<String,String>();
        int _totalElement = _fruits.length;
        for(int x=0;x<_distinct.length;x++){
            for(int i=0;i<_fruits.length;i++){
                if(_distinct[x].equals(_fruits[i])){
                    ++_occurrence;
                }
            }
            double _calPercentage = Math.round((((double)_occurrence/(double)_totalElement)*100));
            _map.put(_distinct[x], String.valueOf(_calPercentage+"%"));
            _occurrence = 0;
        }
        System.out.println(_map);
qasim azam
  • 38
  • 1
  • 1
  • 5
0

Let's see how to find distinct values from an array.

  public class Distinct  {
        public static void main(String args[]) {
             int num[]={1,4,3,2,6,7,4,2,1,2,8,6,7};
                for(int i=0; i<num.length; i++){
                    boolean isdistinct = true;
                    for(int j=0; j<i; j++){
                        if(num[i] == num[j]){
                            isdistinct =false;
                            break;
                        }
                   }
                    if(isdistinct){
                        System.out.print(num[i]+" ");
                    }
               }
           }
     }
0
int arr[] = {1,1,2,2,3,3,4,5,5,6}; 
for(int i=0; i<arr.length; i++) {
int count = 0;
    for(int j=0; j<arr.length; j++) {
        if ((arr[i] == arr[j]) && (i!=j)) {
            count++ ;
        }
    }
        if(count==0) {
            System.out.println(arr[i]);
        }
}
0
public static int[] findAndReturnUniqueIntArray(int[] arr) {
    int[] distinctElements = {};
    int newArrLength = distinctElements.length;
    for (int i = 0; i < arr.length; i++) {
        boolean exists = false;
        if (distinctElements.length == 0) {
            distinctElements = new int[1];
            distinctElements[newArrLength] = arr[i];
            newArrLength++;
        }

        else {
            for (int j = 0; j < distinctElements.length; j++) {
                if (arr[i] == distinctElements[j]) {
                    exists = true;
                    break;
                }
            }
            if (exists == false) {
                distinctElements = Arrays.copyOf(distinctElements, distinctElements.length+1);
                distinctElements[distinctElements.length-1] = arr[i];
                newArrLength++;
                exists = false;
            }
        }
    }
    return distinctElements;
}
-2

In python you can use Set.

s = Set()
data_list = [1,2,3,4]
s.update(data_list)