0

i am trying to remove duplicate values from my array present i my android project my array is,

String [] standard = {"7", "7", "8", "7", "8", "7", "6"};

i want only unique values that will be stored in an array.like {7, 8, 6}. i am trying to solev this with comparing each element of array with itself and then add it into array. i have tried googling to solve this but i am failed to solve this. means offcourse i am mistaking at somewhere.

how to solve this using the same way that i am trying to solve.

1615903
  • 32,635
  • 12
  • 70
  • 99
neeta
  • 43
  • 12
  • try this http://stackoverflow.com/a/203992/1849024 – imsome1 Feb 21 '17 at 10:48
  • use set from collections – Vivek Mishra Feb 21 '17 at 10:49
  • please google and try to find answers on stack overflow first, before posting a duplicate question. – VijayD Feb 21 '17 at 10:57
  • Possible duplicate of [How do I remove repeated elements from ArrayList?](http://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist) Note : Passing from the Array to an ArrayList is easy – AxelH Feb 21 '17 at 11:11
  • Possible duplicate of [How to get unique values from array](http://stackoverflow.com/questions/13796928/how-to-get-unique-values-from-array) – Hamid Reza Feb 26 '17 at 09:09

4 Answers4

4

You haven't posted why you are using the array but one solution of the problem is to use set data structure. As follows. Set doesn't allow inserting duplicate values.

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

  setExam.add("1");
  setExam.add("2");
  setExam.add("1");

you can also convert array to set as follows

Set<T> mySet = new HashSet<T>(Arrays.asList(someArray));

Set will contain only values 1 and 2. It will NOT contain duplicate values.

v4_adi
  • 1,503
  • 1
  • 12
  • 14
0

according to this link link

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(new String[0]);
Community
  • 1
  • 1
Hamid Reza
  • 624
  • 7
  • 23
  • What are you adding to [Bohemian's answer](http://stackoverflow.com/a/13797185/4391450) ? You can flag for duplicate if you want (I think you have enough reputation) or comment with this answer link. – AxelH Feb 21 '17 at 11:14
0
HashSet set=new HashSet();   
String [] standard = {"7", "7", "8", "7", "8", "7", "6"};
for(int i=0;i<standard.length;i++){
    set.add(standard[i]);
}
Iterator iter = set.iterator();
while(iter.hasNext()) {
    System.out.println(iter.next());
}
Naman Arora
  • 1
  • 1
  • 2
0

If you want to do this with the help of array only, you can use this.

1) Sort the string array.
2) Use another array to store ans.
3) Store first element of your standard array to new array.
4) Compare from second element of the array, and if two elements are different add that element to ans array.

See the code.

public static void main(String args[]) throws Exception
{       
    String [] standard = {"7", "7", "8", "7", "8", "7", "6"};

    System.out.println();

    Arrays.sort(standard);

    String Ans[] = new String[100];

    int k = 0;

    Ans[k++] = standard[0];

    for(int i = 1 ; i < standard.length ; i++)
    {
        if(!standard[i-1].equals(standard[i]))
            Ans[k++] = standard[i];
    }

    for(int i = 0 ; i < 100 && Ans[i] != null; i++)
        System.out.print(Ans[i] + " ");
}