如何从数组中获取唯一项?
我是Java初学者,我找到了一些关于这个主题的主题,但没有一个对我有用。 我有一个像这样的数组:
int[] numbers = {1, 1, 2, 1, 3, 4, 5};
我需要得到这个输出:
1, 2, 3, 4, 5
该数组中的每个项目只需一次。
但是如何得到它呢?
最简单的解决方案,无需编写自己的算法:
Integer[] numbers = {1, 1, 2, 1, 3, 4, 5}; Set uniqKeys = new TreeSet (); uniqKeys.addAll(Arrays.asList(numbers)); System.out.println("uniqKeys: " + uniqKeys);
设置接口保证值的唯一性。 TreeSet还对此值进行排序。
您可以使用Set
并节省大量时间,因为它包含唯一元素。 如果不允许使用Java Collections中的任何类,请对数组进行排序并计算唯一元素。 您可以手动对数组进行排序或使用Arrays#sort
。
我将发布Set
代码:
int[] numbers = {1, 1, 2, 1, 3, 4, 5}; Set setUniqueNumbers = new LinkedHashSet (); for(int x : numbers) { setUniqueNumbers.add(x); } for(Integer x : setUniqueNumbers) { System.out.println(x); }
请注意,我更喜欢使用LinkedHashSet
作为Set实现,因为它维护了元素的插入顺序。 这意味着,如果您的数组是{2 , 1 , 2}
那么输出将是2, 1
而不是1, 2
。
在Java 8中:
final int[] expected = { 1, 2, 3, 4, 5 }; final int[] numbers = { 1, 1, 2, 1, 3, 4, 5 }; final int[] distinct = Arrays.stream(numbers) .distinct() .toArray(); Assert.assertArrayEquals(Arrays.toString(distinct), expected, distinct); final int[] unorderedNumbers = { 5, 1, 2, 1, 4, 3, 5 }; final int[] distinctOrdered = Arrays.stream(unorderedNumbers) .sorted() .distinct() .toArray(); Assert.assertArrayEquals(Arrays.toString(distinctOrdered), expected, distinctOrdered);
//Running total of distinct integers found int distinctIntegers = 0; for (int j = 0; j < array.length; j++) { //Get the next integer to check int thisInt = array[j]; //Check if we've seen it before (by checking all array indexes below j) boolean seenThisIntBefore = false; for (int i = 0; i < j; i++) { if (thisInt == array[i]) { seenThisIntBefore = true; } } //If we have not seen the integer before, increment the running total of distinct integers if (!seenThisIntBefore) { distinctIntegers++; } }
下面的代码将打印出唯一的整数看看:
printUniqueInteger(new int[]{1, 1, 2, 1, 3, 4, 5}); static void printUniqueInteger(int array[]){ HashMap map = new HashMap(); for(int i = 0; i < array.length; i++){ map.put(array[i], "test"); } for(Integer key : map.keySet()){ System.out.println(key); } }
public class Practice { public static void main(String[] args) { List list = new LinkedList<>(Arrays.asList(3,7,3,-1,2,3,7,2,15,15)); countUnique(list); } public static void countUnique(List list){ Collections.sort(list); Set uniqueNumbers = new HashSet (list); System.out.println(uniqueNumbers.size()); }
}
Simple Hashing将比任何Java内置函数更 高效 , 更快速 :
public class Main { static int HASH[]; public static void main(String[] args) { int[] numbers = {1, 1, 2, 1, 3, 4, 5}; HASH=new int[100000]; for(int i=0;i
时间复杂度 :O(N),其中N =数字长度
DEMO
你可以这样做:
int[] numbers = {1, 1, 2, 1, 3, 4, 5}; ArrayList store = new ArrayList (); // so the size can vary for (int n = 0; n < numbers.length; n++){ if (!store.contains(numbers[n])){ // if numbers[n] is not in store, then add it store.add(numbers[n]); } } numbers = new int[store.size()]; for (int n = 0; n < store.size(); n++){ numbers[n] = store.get(n); }
整数和整数可以(几乎)互换使用。 这段代码将您的数组“数字”并更改它,以便丢失所有重复的数字。 如果要对其进行排序,可以添加Collections.sort(store);
在numbers = new int[store.size()]
我不知道你是否已经解决了你的问题,但我的代码是:
int[] numbers = {1, 1, 2, 1, 3, 4, 5}; int x = numbers.length; int[] unique = new int[x]; int p = 0; for(int i = 0; i < x; i++) { int temp = numbers[i]; int b = 0; for(int y = 0; y < x; y++) { if(unique[y] != temp) { b++; } } if(b == x) { unique[p] = temp; p++; } } for(int a = 0; a < p; a++) { System.out.print(unique[a]); if(a < p-1) { System.out.print(", "); } }
String s1[]= {"hello","hi","j2ee","j2ee","sql","jdbc","hello","jdbc","hybernet","j2ee"}; int c=0; for(int i=0;i
要找出独特的数据:
public class Uniquedata { public static void main(String[] args) { int c=0; String s1[]={"hello","hi","j2ee","j2ee","sql","jdbc","hello","jdbc","hybernet","j2ee","hello","hello","hybernet"}; for(int i=0;i
您可以使用
Object[] array = new HashSet<>(Arrays.asList(numbers)).toArray();
这是我使用计数排序的代码(部分)
输出是由独特元素组成的排序数组
void findUniqueElementsInArray(int arr[]) { int[] count = new int[256]; int outputArrayLength = 0; for (int i = 0; i < arr.length; i++) { if (count[arr[i]] < 1) { count[arr[i]] = count[arr[i]] + 1; outputArrayLength++; } } for (int i = 1; i < 256; i++) { count[i] = count[i] + count[i - 1]; } int[] sortedArray = new int[outputArrayLength]; for (int i = 0; i < arr.length; i++) { sortedArray[count[arr[i]] - 1] = arr[i]; } for (int i = 0; i < sortedArray.length; i++) { System.out.println(sortedArray[i]); } }
参考 - 在尝试解决HackerEarth的问题时发现了这个解决方案
public class DistinctArray { public static void main(String[] args) { int num[]={1,2,5,4,1,2,3,5}; for(int i =0;i