从整数数组中删除重复项

我有编码的问题:

编写一个名为removeDuplicates的静态方法,该方法将整数数组作为输入,并返回一个新的整数数组,并删除所有重复项。 例如,如果输入数组具有元素{4,3,3,4,5,2,4},则生成的数组应为{4,3,5,2}

这是我到目前为止所做的

 public static int[] removeDuplicates(int []s){ int [] k = new int[s.length]; k[0]=s[0]; int m =1; for(int i=1;i<s.length;++i){ if(s[i]!=s[i-1]){ k[m]=s[i]; ++m; }//endIF }//endFori return k; }//endMethod 

尝试这个 –

 public static int[] removeDuplicates(int []s){ int result[] = new int[s.length], j=0; for (int i : s) { if(!isExists(result, i)) result[j++] = i; } return result; } private static boolean isExists(int[] array, int value){ for (int i : array) { if(i==value) return true; } return false; } 

要保留排序并删除整数数组中的重复项,可以尝试以下操作:

 public void removeDupInIntArray(int[] ints){ Set setString = new LinkedHashSet(); for(int i=0;i 

希望这可以帮助。

首先,你应该知道长度没有重复(重复):初始长度减去重复次数。 然后创建具有正确长度的新数组。 然后检查list []的每个元素是否为dup,如果已创建dup – 检查下一个元素,如果dup未建立 – 将元素复制到新数组。

 public static int[] eliminateDuplicates(int[] list) { int newLength = list.length; // find length w/o duplicates: for (int i = 1; i < list.length; i++) { for (int j = 0; j < i; j++) { if (list[i] == list[j]) { // if duplicate founded then decrease length by 1 newLength--; break; } } } int[] newArray = new int[newLength]; // create new array with new length newArray[0] = list[0]; // 1st element goes to new array int inx = 1; // index for 2nd element of new array boolean isDuplicate; for (int i = 1; i < list.length; i++) { isDuplicate = false; for (int j = 0; j < i; j++) { if (list[i] == list[j]) { // if duplicate founded then change boolean variable and break isDuplicate = true; break; } } if (!isDuplicate) { // if it's not duplicate then put it to new array newArray[inx] = list[i]; inx++; } } return newArray; } 

也许你可以使用lambdaj( 在这里下载 , 网站 ),这个库非常强大,用于管理集合(..list,数组),下面的代码非常简单,工作完美:

 import static ch.lambdaj.Lambda.selectDistinct; import java.util.Arrays; import java.util.List; public class DistinctList { public static void main(String[] args) { List numbers = Arrays.asList(1,3,4,2,1,5,6,8,8,3,4,5,13); System.out.println("List with duplicates: " + numbers); System.out.println("List without duplicates: " + selectDistinct(numbers)); } } 

此代码显示:

 List with duplicates: [1, 3, 4, 2, 1, 5, 6, 8, 8, 3, 4, 5, 13] List without duplicates: [1, 2, 3, 4, 5, 6, 8, 13] 

在一行中,您可以获得一个不同的列表,这是一个简单的示例,但使用此库可以解决更多问题。

 selectDistinct(numbers) 

您必须将lambdaj-2.4.jar添加到您的项目中。 我希望这会有用。

注意:这将帮助您假设您可以选择代码。

 public int[] removeRepetativeInteger(int[] list){ if(list.length == 0){ return null; } if(list.length == 1){ return list; } ArrayList numbers = new ArrayList<>(); for(int i = 0; i< list.length; i++){ if (!numbers.contains(list[i])){ numbers.add(list[i]); } } Iterator valueIterator = numbers.iterator(); int[] resultArray = new int[numbers.size()]; int i = 0; while (valueIterator.hasNext()) { resultArray[i] = valueIterator.next(); i++; } return resultArray; } 

你要做的是,你必须检查第二个数组中的每个元素是否已存在前一个元素。

您可以使用更好的方法使用HashSet和返回集。

 public static Set removeDuplicates(int []s){ Set set = new HashSet(); for(int i=0;i 

如果你需要int Array而不是看看这个java-hashsetinteger-to-int-array链接。

迭代数组并填充集合,因为集合不能包含重复项。 然后将集合中的元素复制到新数组中并返回它。 如下所示:

 public static int[] removeDuplicates(int[] array) { // add the ints into a set Set set = new HashSet(); for (int i = 0; i < array.length; i++) { set.add(array[i]); } // copy the elements from the set into an array int[] result = new int[set.size()]; int i = 0; for (Integer u : set) { result[i++] = u; } return result; } 

尝试这个

 public static int[] removeDuplicates(int[] s) { Integer[] array = new HashSet(Arrays.asList(ArrayUtils.toObject(s))).toArray(new Integer[0]); return ArrayUtils.toPrimitive(array); } 

编辑:使用Apache Lang更新以转换为基元。

您也可以使用Google的Guava库并使用ImmutableSet

 ImmutableSet.copyOf(myArray).asList(); 
 public class Test static int[] array = {4, 3, 3, 4, 5, 2, 4}; static HashSet list = new HashSet(); public static void main(String ar[]) { for(int i=0;i 

输出为: [2, 3, 4, 5]

您还可以将数组元素放入一个Set中,语义恰恰是它不包含重复元素。

你可以天真地做。 首先,您需要对数组进行排序。 您可以使用任何排序算法来完成。 我确实使用了快速排序。 然后检查下一个位置的位置。 如果它们不相同,请在新数组中添加值,否则跳过此迭代。

示例代码(快速排序):

  public static void quickSort(int[] array, int low, int high) { int i = low; int j = high; int pivot = array[low + (high - low) / 2]; while (i <= j) { while (array[i] < pivot) i++; while (array[j] > pivot) j--; if (i <= j) { exchange(array, i, j); i++; j--; } } if (0 < j) quickSort(array, 0, j); if (i < high) quickSort(array, i, high); } public static void exchange(int[] array, int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } 

删除重复项:

  public static int[] removeDuplicate(int[] arrays) { quickSort(arrays, 0, arrays.length - 1); int[] newArrays = new int[arrays.length]; int count = 0; for (int i = 0; i < arrays.length - 1; i++) { if (arrays[i] != arrays[i + 1]) { newArrays[count] = arrays[i]; count++; } } return newArrays; } 

您可以使用不允许重复元素的HashSet

 public static void deleteDups(int a []) { HashSet numbers = new HashSet(); for(int n : a) { numbers.add(n); } for(int k : numbers) { System.out.println(k); } System.out.println(numbers); } public static void main(String[] args) { int a[]={2,3,3,4,4,5,6}; RemoveDuplicate.deleteDups(a); } } o/p is 2 3 4 5 6 

[2,3,4,5,6]

 public class DistinctNumbers{ public static void main(String[] args){ java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter ten numbers: "); int[] numbers = new int[10]; for(int i = 0; i < numbers.length; ++i){ numbers[i] = input.nextInt(); } System.out.println("The distinct numbers are:"); System.out.println(java.util.Arrays.toString(eliminateDuplicates(numbers))); } public static int[] eliminateDuplicates(int[] list){ int[] distinctList = new int[list.length]; boolean isDuplicate = false; int count = list.length-1; for(int i = list.length-1; i >= 0; --i){ isDuplicate = false; for(int j = i-1; j >= 0 && !isDuplicate; --j){ if(list[j] == list[i]){ isDuplicate = true; } } if(!isDuplicate){ distinctList[count--] = list[i]; } } int[] out = new int[list.length-count-1]; System.arraycopy(distinctList, count+1, out, 0, list.length-count-1); return out; } } 

嘿所有你可以使用我创建的代码!

 import java.util.*; public class DistinctNumber { public static void main(String[] args) { int[] nums= {1,3,2,3,4,3,2,5,4,6}; int [] T2 = duplicate(nums); for (int i = 0; i < T2.length; i++) { System.out.println(T2[i]); } } public static boolean exist(int x,int []A){ for (int i = 0; i < A.length; i++) { if(x==A[i]){ return true; } } return false; } public static int [] EliminateDuplicate(int [] numbers){ int [] B = new int[numbers.length]; int i=0,j=0; for(i=0;i 

这对我有用:

 import java.util.Arrays; import java.util.HashSet; public class Util { public static int[] removeDups(final int[] intArrayWithDups) { final int[] intArrayDupsRemoved = new int[intArrayWithDups.length]; final HashSet alreadyAdded = new HashSet<>(); int innerCounter = 0; for (int integer : intArrayWithDups) { if (!alreadyAdded.contains(integer)) { intArrayDupsRemoved[innerCounter] = integer; alreadyAdded.add(intArrayDupsRemoved[innerCounter]); innerCounter++; } } return Arrays.copyOf(intArrayDupsRemoved, innerCounter); } } 

你可以做这样的事情

  public class MyClass { public static void main(String args[]) { int[] man = {4,56,98,89,78,45,78, 79, 56}; for (int i = 0; i < man.length; i++) { for (int j = i+1; j < man.length; j++) { //check if it is equal if (man[i] == man[j]) { man[j] = man[j] -1; //Decrementing size j--; } } } //Array without duplicates for(int k=0; k 

这是一个面试问题。 问题:从arrays中删除重复项:

 public class Solution4 { public static void main(String[] args) { int[] a = {1,1,2,3,4,5,6,6,7,8}; int countwithoutDuplicates = lengthofarraywithoutDuplicates(a); for(int i = 0 ; i < countwithoutDuplicates ; i++) { System.out.println(a[i] + " "); } } private static int lengthofarraywithoutDuplicates(int[] a) { int countwithoutDuplicates = 1 ; for (int i = 1; i < a.length; i++) { if( a[i] != a[i-1] ) { a[countwithoutDuplicates++] = a[i]; }//if }//for System.out.println("length of array withpout duplicates = >" + countwithoutDuplicates); return countwithoutDuplicates; }//lengthofarraywithoutDuplicates } 

在Python中:

 def lengthwithoutduplicates(nums): if not nums: return 0 if len(nums) == 1:return 1 # moving backwards from last element ielen(a) -1 to first element 0 and step is -1 for i in range(len(nums)-1,0,-1): # delete the repeated element if nums[i] == nums[i-1]: del nums[i] # store the new length of the array without the duplicates in a variable # and return the variable l = len(a) return l a = [1, 1, 2, 3, 4, 5, 6, 6, 7, 8]; l = lengthwithoutduplicates(a) for i in range(1,l):print(i) 

在Python中:使用枚举的列表理解

 a = [1, 1, 2, 3, 4, 5, 6, 6, 7, 8] aa = [ ch for i, ch in enumerate(a) if ch not in a[:i] ] print(aa) # output => [1, 2, 3, 4, 5, 6, 7, 8] 
 import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; // Remove duplicates from a list of integers public class IntegerUtils { public static void main(String[] args) { int intArray[] = {1, 2, 4, 2, 67, 4, 9}; List uniqueList = removeDuplicates(intArray); uniqueList.stream().forEach(p -> System.out.println(p)); } public static List removeDuplicates(int[] intArray) { return Arrays.stream(intArray).boxed().distinct().collect(Collectors.toList()); } } 

尝试这个。

  int numbers[] = {1,2,3,4,1,2,3,4,5,1,2,3,4}; numbers = java.util.stream.IntStream.of(numbers).distinct().toArray(); 
 int[] arrayRemoveDuplicates= Arrays.stream("Array").distinct().toArray(); // print the unique array for (int i = 0; i < arrayRemoveDuplicates.length; i++) { System.out.println(arrayRemoveDuplicates[i]); } 

java.util.streamapi在java 8中引入

import java.util。*;

public class Duplicates {

 public static void main(String[] args) { // TODO Auto-generated method stub int [] myArray = {1,3,2,4,3,2,3,4,5,6,7,6,5,4,3,4,5,6,76,5,4,3,4,4,5}; List  myList = new ArrayList (); myList = removeDuplicates(myArray); //Printing Output for (int k=0; k removeDuplicates(int[] myArray) { // TODO Auto-generated method stub Arrays.sort(myArray); List  myList = new ArrayList (); for (int i=0; i 

}

公共课Foo {

 public static void main(String[] args) { //example input int input[] = new int[]{1, 6 , 5896, 5896, 9, 100,7, 1000, 8, 9, 0, 10, 90, 4}; //use list because the size is dynamical can change List result = new ArrayList(); for(int i=0; i 

输出:1,6,589,9,100,7,1000,8,0,10,90,4,