Array of unique elements?

后端 未结 5 2072
时光取名叫无心
时光取名叫无心 2020-12-06 05:52

Given an array like the one below, I was wondering if there is an easy way to turn this array into an array with unique values only?

This is given:

         


        
5条回答
  •  离开以前
    2020-12-06 06:34

    Supposing an array of Objects:

    Object[] arr;
    
    {...omissis...}
    
    List list = new ArrayList();
    for(Object val: arr) {
      if(!list.contains(val)) {
        list.add(val);
      }
    }
    list.toArray(new Object[0]);
    
    
    

    Replace Object with your Array Class if needed.

    提交回复
    热议问题