[英]How to get unique values from array
我有一個數組,我想從中刪除重復項。
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);
但這並沒有給我Array的獨特價值。
我的陣列: - {英文,法文,日文,俄文,中文字幕,英文,法文,日文,俄文,中文字幕}
輸出: - [日語,俄語,法語,中文字幕],中文字幕,[英語,英語]
你可以在java 7中的一行中完成它:
String[] unique = new HashSet<String>(Arrays.asList(array)).toArray(new String[0]);
在Java 8中更簡單,更簡單:
String[] unique = Arrays.stream(array).distinct().toArray(String[]::new);
HashSet將完成這項工作。
你可以試試這個:
List<String> newList = new ArrayList<String>(new HashSet<String>(oldList));
使用Java 8的Stream API,這是一個具有通用Array類型的解決方案:
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);
}
});
}
它適用於任何Object類型數組,但不適用於原始數組。
對於原始數組,它看起來像這樣:
public static int[] makeUnique(int... values)
{
return Arrays.stream(values).distinct().toArray();
}
最后這是一個小單元測試:
@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 }));
}
您可以獲得兩組,一組包含所有字幕,另一組包含副本
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);
}
嘗試而不是這個
Set<String> set = new HashSet<String>();
打電話給這個
set.addAll(trimmedArray);
為什么首先將項添加到數組中然后將其轉換為字符串? 只需遍歷tha數組並將它們復制到Set.Then打印包含唯一值的新創建集。
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);
}
此代碼將計算數組中的不同元素,然后查找它們的出現。 並計算百分比並將其保存到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);
讓我們看看如何從數組中找到不同的值。
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]+" ");
}
}
}
}
如果您不想在上面提到的Java8中使用Hashset或新方法,您可以編寫此代碼,首先需要對數組進行排序,以便相似的值將彼此相鄰,然后計算相鄰單元格中不同對的數量。
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;
}
在python中,您可以使用Set。
s = Set()
data_list = [1,2,3,4]
s.update(data_list)
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]);
}
}
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.