リスト内の重複を特定する


119

整数型のリストがあります。例:

[1, 1, 2, 3, 3, 3]

すべての重複を返すメソッドが欲しいです:

[1, 3]

これを行う最良の方法は何ですか?


2
入力リストは(例のように)ソートされることが保証されていますか?
NPE、2011

7
リストをソートしてから、現在の値と以前の値を保持したままウォークします。現在の==前であれば、重複があります。
mcfinnigan 2011

いいえ、リストは必ずしもソートされていません。
最新の2011

回答:


183

のメソッドaddSet、値がすでに存在するかどうかに関係なくブール値を返します(存在しない場合はtrue、既に存在する場合はfalse 。セットのドキュメントを参照)。

したがって、すべての値を反復処理するだけです。

public Set<Integer> findDuplicates(List<Integer> listContainingDuplicates)
{ 
  final Set<Integer> setToReturn = new HashSet<>(); 
  final Set<Integer> set1 = new HashSet<>();

  for (Integer yourInt : listContainingDuplicates)
  {
   if (!set1.add(yourInt))
   {
    setToReturn.add(yourInt);
   }
  }
  return setToReturn;
}

1
なぜsetToReturnがあるのですか?set1.add(yourInt)を使用してset1を返すことはできませんか?
Phil

3
はい、正確に。ただし、指定されたリストにelemntが1度しか存在しない場合は、要素も追加されます。質問の例を見てください。数値2がsetToReturnではなくset1に挿入されているため、私のソリューションは[1,3]を返します。あなたのソリューションは[1,2,3]を返します(これは要件ではありません)
leifg

1
for (Integer yourInt特に入力にすでにが含まれているため、不要なボックス化とボックス化解除を避けるために、を使用することをお勧めしますInteger
Hosam Aly、2011

1
@JonasThelemannセットの全体的な考え方は、重複を含むことはできないということです。したがって、3を追加する頻度に関係なく、常に1回だけになります。
leifg 2016年

1
ちなみにHashSet、たとえば初期容量としてを指定した場合など、負荷係数も考慮する必要がある場合は100、その数の要素を追加するため、次の2の累乗(128)に丸められます。これは、デフォルトの負荷係数がの0.75f場合、サイズ変更のしきい値はになる96ため、100要素を追加する前にサイズ変更が行われます。ありがたいことに、サイズ変更はもはやそれほど高価ではありません。最新のJREでは、サイズ変更はもうリハッシュではありません。要素は、関連するビットに基づいて、2つの可能な結果の場所に分散されます。
Holger

50

これに対する解決策も必要でした。私はライフフのソリューションを使用し、それを一般化しました。

private <T> Set<T> findDuplicates(Collection<T> collection) {

    Set<T> duplicates = new LinkedHashSet<>();
    Set<T> uniques = new HashSet<>();

    for(T t : collection) {
        if(!uniques.add(t)) {
            duplicates.add(t);
        }
    }

    return duplicates;
}

1
私はこれが3年後であることを知っていますが、なぜLinkedHashedSet、つまりなぜ注文を気にするのですか?
Ahmad Ragab 2016

4
@AhmadRagab正解です。重複が見つかった順序を気にしない限り、LinkedHashSetは必要ありません(私は当時私が行っていたと思います)
John Strickler '19

フォローアップありがとうございます!
Ahmad Ragab

(入力を並べ替えるのではなく)この最適なソリューションを使用したい入力データが多すぎる場合は、HashSet()オブジェクトのサイズを事前に割り当てることもできます。事前割り当てがないと、HashSet()に挿入するときにO(1)挿入時間が得られません。これは、内部ハッシュ配列のサイズが繰り返し変更されるためです。その後、挿入は平均してO(log N)時間のようなものになります。つまり、N個のアイテムすべてを処理すると、O(N)であった可能性があるときにO(N log N)になります。
johnstosh 2017

39

John Stricklerのソリューションを取り、JDK8で導入されたストリームAPIを使用するように作り直しました。

private <T> Set<T> findDuplicates(Collection<T> collection) {
    Set<T> uniques = new HashSet<>();
    return collection.stream()
        .filter(e -> !uniques.add(e))
        .collect(Collectors.toSet());
}

3
これはちょっと読みにくいですよね?あなたはストリーム操作で副作用を起こしているので、それを考えるのはちょっと難しいです。しかし、それは私が機能的なスタイルを考えているだけです。簡潔ですが、おそらく最短の方法です;)。
froginvasion

大きなリストの場合、これを複数のスレッドで並行して実行できますか?
johnstosh 2017

@froginvasion組み込みdistinct()メソッドもステートフルです。ステートフルではない効率的な(O(n))個別の操作を考えることはできません。
wilmol

18

これはJava 8でStreamsを使用するソリューションです

// lets assume the original list is filled with {1,1,2,3,6,3,8,7}
List<String> original = new ArrayList<>();
List<String> result = new ArrayList<>();

このオブジェクトの頻度がリストに複数あるかどうかを確認します。次に.distinct()を呼び出して、結果に一意の要素のみを含めます

result = original.stream()
    .filter(e -> Collections.frequency(original, e) > 1)
    .distinct()
    .collect(Collectors.toList());
// returns {1,3}
// returns only numbers which occur more than once

result = original.stream()
    .filter(e -> Collections.frequency(original, e) == 1)
    .collect(Collectors.toList());
// returns {2,6,8,7}
// returns numbers which occur only once

result = original.stream()
    .distinct()
    .collect(Collectors.toList());
// returns {1,2,3,6,8,7}
// returns the list without duplicates

1
これは読みやすさの点では優れていますが、パフォーマンスには本当に悪いです。Collections::frequencyO(n)です。アイテムの頻度を見つけるには、コレクション全体を調べる必要があります。そして、これをコレクション内のすべてのアイテムに対して1回呼び出しますO(n^2)。これにより、これらのスニペットが作成されます。一握り以上の要素のコレクションの違いに気付くでしょう。これを実際のコードで使用することはありません。
Pawan

13

Java 8基本ソリューション:

List duplicates =    
list.stream().collect(Collectors.groupingBy(Function.identity()))
    .entrySet()
    .stream()
    .filter(e -> e.getValue().size() > 1)
    .map(Map.Entry::getKey)
    .collect(Collectors.toList());

入力リストはマップに変換されます(同じ値によるグループ化)。その後、一意の値を持つマップの値は、キーを使用してマッピングし、その後、リストのリストはリストに変換され、「削除」されている
بلالالمصمودي

美しく高速なソリューション、アイテムの特定のゲッターでのフィルタリングに直接変更可能
arberg

11
int[] nums =  new int[] {1, 1, 2, 3, 3, 3};
Arrays.sort(nums);
for (int i = 0; i < nums.length-1; i++) {

    if (nums[i] == nums[i+1]) {
        System.out.println("duplicate item "+nums[i+1]+" at Location"+(i+1) );
    }

}

明らかに、印刷する代わりにそれらを使って何でも好きなことができます(つまり、重複する値の一意のリストを取得するためにセットに入れます)...これには、重複するアイテムの場所を記録するという利点もあります。


7

Java 8でGuavaを使用する

private Set<Integer> findDuplicates(List<Integer> input) {
    // Linked* preserves insertion order so the returned Sets iteration order is somewhat like the original list
    LinkedHashMultiset<Integer> duplicates = LinkedHashMultiset.create(input);

    // Remove all entries with a count of 1
    duplicates.entrySet().removeIf(entry -> entry.getCount() == 1);

    return duplicates.elementSet();
}

7

これも機能します:

public static Set<Integer> findDuplicates(List<Integer> input) {
    List<Integer> copy = new ArrayList<Integer>(input);
    for (Integer value : new HashSet<Integer>(input)) {
        copy.remove(value);
    }
    return new HashSet<Integer>(copy);
}

機能しますが、配列リストでのremove()の呼び出しは線形検索であるため、かなり低速です。
johnstosh 2017

本当。入力に多くの重複が含まれている場合、それらの数が少ない場合よりもパフォーマンスへの影響が少ないことに注意してください。
Adriaan Koster 2017

6

次のようなものを使用できます。

List<Integer> newList = new ArrayList<Integer>();
for(int i : yourOldList)
{
    yourOldList.remove(i);
    if(yourOldList.contains(i) && !newList.contains(i)) newList.add(i);
}

2
ここでのリストの使用は非常に効果的ではありません
Alexander Farber '20

2
そして、intここで変数型としての使用を開始しないでください。つまり、すべての反復で、Integerのボックス化が1回解除され、intのボックス化が4回実行されます。
Sean Patrick Floyd

1
反復しながらリストから要素を削除しようとすると、ConcurrentModificationExceptionを簡単に取得できると思います
Jad B.

1
リストを繰り返し処理し、その場で要素を削除するため、これは100%ConcurrentModificationExceptionです。
theo231022 2016年

5

ランバは解決策かもしれない

Integer[] nums =  new Integer[] {1, 1, 2, 3, 3, 3};
List<Integer> list = Arrays.asList(nums);

List<Integer> dps = list.stream().distinct().filter(entry -> Collections.frequency(list, entry) > 1).collect(Collectors.toList());

1
機能しますが、各エントリに対してCollections.frequencyを実行するため、速度が低下します。
arberg

4

MultiMapを使用して、各値をキー/値セットとして保存します。次に、キーを反復処理して、複数の値を持つキーを見つけます。


3

Eclipseコレクションを使用する場合、これは機能します。

MutableList<Integer> list = Lists.mutable.with(1, 1, 2, 3, 3, 3);
Set<Integer> dupes = list.toBag().selectByOccurrences(i -> i > 1).toSet();
Assert.assertEquals(Sets.mutable.with(1, 3), dupes);

更新: Eclipseコレクション9.2以降で使用できるようになりましたselectDuplicates

MutableList<Integer> list = Lists.mutable.with(1, 1, 2, 3, 3, 3);
Set<Integer> dupes = list.toBag().selectDuplicates().toSet();
Assert.assertEquals(Sets.mutable.with(1, 3), dupes);

これを達成するために、プリミティブコレクションを使用することもできます。

IntList list = IntLists.mutable.with(1, 1, 2, 3, 3, 3);
IntSet dupes = list.toBag().selectDuplicates().toSet();
Assert.assertEquals(IntSets.mutable.with(1, 3), dupes);

注:私はEclipseコレクションのコミッターです。


2
public class practicese {
       public static void main(String[] args) {   

           List<Integer> listOf = new ArrayList<Integer>();
           listOf.add(3);
           listOf.add(1);
           listOf.add(2);
           listOf.add(3);
           listOf.add(3);
           listOf.add(2);
           listOf.add(1);

           List<Integer> tempList = new ArrayList<Integer>();
           for(Integer obj:listOf){
                if(!tempList.contains(obj)){
                    tempList.add(obj);

                }
            }
            System.out.println(tempList);

    }

}

私はこの答えが好きです。別のリストに重複を保存するには、elseを追加するだけです。ありがとう
ティアゴマチャド

2

ここでのいくつかの回答に似ていますが、いくつかのプロパティに基づいて重複を見つけたい場合:

  public static <T, R> Set<R> findDuplicates(Collection<? extends T> collection, Function<? super T, ? extends R> mapper) {
    Set<R> uniques = new HashSet<>();
    return collection.stream()
        .map(mapper)
        .filter(e -> !uniques.add(e))
        .collect(toSet());
  }

1

を作成しMap<Integer,Integer>、要素がマップにある場合はリストを反復処理し、その値を増やします。それ以外の場合は、key = 1でマップに追加し、マップを
反復処理し、key> = 2ですべての要素をリストに追加します

public static void main(String[] args) {
        List<Integer> list = new LinkedList<Integer>();
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(3);
        Map<Integer,Integer> map = new HashMap<Integer, Integer>();
        for (Integer x : list) { 
            Integer val = map.get(x);
            if (val == null) { 
                map.put(x,1);
            } else {
                map.remove(x);
                map.put(x,val+1);
            }
        }
        List<Integer> result = new LinkedList<Integer>();
        for (Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() > 1) {
                result.add(entry.getKey());
            }
        }
        for (Integer x : result) { 
            System.out.println(x);
        }

    }

これはかなり良いです。重複がいくつあったかを知る必要がある場合は、これが最適なソリューションです。注意事項:(1)put()を実行する前にremove()を呼び出す必要はありません。(2)add()呼び出しを繰り返し使用するのではなく、配列からLinkedListを設定できます。(3)val!= nullの場合、すぐに結果にxを追加できます。結果は、重複の数をカウントするかどうかに応じて、セットまたはリストになります。
johnstosh

1

トップアンサーのコンパクトなジェネリファイバージョン、空のチェックと事前割り当てされたセットサイズも追加:

public static final <T> Set<T> findDuplicates(final List<T> listWhichMayHaveDuplicates) {
    final Set<T> duplicates = new HashSet<>();
    final int listSize = listWhichMayHaveDuplicates.size();
    if (listSize > 0) {
      final Set<T> tempSet = new HashSet<>(listSize);
      for (final T element : listWhichMayHaveDuplicates) {
        if (!tempSet.add(element)) {
          duplicates.add(element);
        }
      }
    }
    return duplicates;
  }

ゼロチェックが必要ですか?新しいHashSet <>(0)は賢明な空のセットを返しますか?
johnstosh 2017

@johnstoshこのコードは簡略化できますが、ゼロをチェックするtempSetlistSize、必要な場合にのみwithを初期化できます。これはマイナーな最適化ですが、私はそれが好きです。
Christophe Roussy 2017

1

私はセバスチャンの答えを受け取り、それにkeyExtractorを追加しました-

    private <U, T> Set<T> findDuplicates(Collection<T> collection, Function<? super T,? extends U> keyExtractor) {
        Map<U, T> uniques = new HashMap<>(); // maps unique keys to corresponding values
        return collection.stream()
            .filter(e -> uniques.put(keyExtractor.apply(e), e) != null)
            .collect(Collectors.toSet());
    }

1

スレッドセーフな代替はこれです:

/**
 * Returns all duplicates that are in the list as a new {@link Set} thread-safe.
 * <p>
 * Usually the Set will contain only the last duplicate, however the decision
 * what elements are equal depends on the implementation of the {@link List}. An
 * exotic implementation of {@link List} might decide two elements are "equal",
 * in this case multiple duplicates might be returned.
 * 
 * @param <X>  The type of element to compare.
 * @param list The list that contains the elements, never <code>null</code>.
 * @return A set of all duplicates in the list. Returns only the last duplicate.
 */
public <X extends Object> Set<X> findDuplicates(List<X> list) {
    Set<X> dups = new LinkedHashSet<>(list.size());
    synchronized (list) {
        for (X x : list) {
            if (list.indexOf(x) != list.lastIndexOf(x)) {
                dups.add(x);
            }
        }
    }
    return dups;
}

0

これを試して、リスト内の重複アイテムを見つけてください:

ArrayList<String> arrayList1 = new ArrayList<String>(); 

arrayList1.add("A"); 
arrayList1.add("A"); 
arrayList1.add("B"); 
arrayList1.add("B"); 
arrayList1.add("B"); 
arrayList1.add("C"); 

for (int x=0; x< arrayList1.size(); x++) 
{ 
System.out.println("arrayList1 :"+arrayList1.get(x)); 
} 
Set s=new TreeSet(); 
s.addAll(arrayList1); 
Iterator it=s.iterator(); 
while (it.hasNext()) 
{ 
System.out.println("Set :"+(String)it.next()); 
} 

はい、これはセットを検出しますが、重複しているアイテムのリストまたはセットは検出しません。
johnstosh 2017

0

これは、ソート済みと未ソートの両方で機能するはずです。

public void testFindDuplicates() {

    List<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(1);
    list.add(2);
    list.add(3);
    list.add(3);
    list.add(3);

    Set<Integer> result = new HashSet<Integer>();
    int currentIndex = 0;
    for (Integer i : list) {
        if (!result.contains(i) && list.subList(currentIndex + 1, list.size()).contains(i)) {
            result.add(i);
        }
        currentIndex++;
    }
    assertEquals(2, result.size());
    assertTrue(result.contains(1));
    assertTrue(result.contains(3));
}

ArrayListのsubListでcontains()を呼び出すと、線形検索であるため負荷が高くなります。したがって、これは10個のアイテムでは大丈夫ですが、1,000万個では大丈夫ではありません。
johnstosh 2017

0

これは、機能的手法が優れている点です。たとえば、次のF#ソリューションは、最高の命令型Javaソリューションよりも明確でバグが発生しにくくなっています(私はJavaとF#の両方で毎日作業しています)。

[1;1;2;3;3;3] 
|> Seq.countBy id 
|> Seq.choose (fun (key,count) -> if count > 1 then Some(key) else None)

もちろん、この質問はJavaに関するものです。だから私の提案は、Javaに機能的な機能をもたらすライブラリを採用することです。たとえば、次のように自分のライブラリを使用して解決できます(他にもいくつかの価値のあるものがあります)。

Seq.of(1,1,2,3,3,3)
.groupBy(new Func1<Integer,Integer>() {
    public Integer call(Integer key) {
        return key;
    }
}).filter(new Predicate<Grouping<Integer,Integer>>() {
   public Boolean call(Grouping<Integer, Integer> grouping) {
        return grouping.getGrouping().count() > 1;
   }
}).map(new Func1<Grouping<Integer,Integer>,Integer>() {
    public Integer call(Grouping<Integer, Integer> grouping) {
        return grouping.getKey();
    }
});

そして、Javaが実際に関数型プログラミングにどれほどの負荷をかけているかがわかります。このような単純な問題のため、Javaで必要なものを表現することは困難です。
froginvasion

0
public class DuplicatesWithOutCollection {

    public static void main(String[] args) {

        int[] arr = new int[] { 2, 3, 4, 6, 6, 8, 10, 10, 10, 11, 12, 12 };

        boolean flag = false;
        int k = 1;
        while (k == 1) {

            arr = removeDuplicate(arr);
            flag = checkDuplicate(arr, flag);
            if (flag) {
                k = 1;
            } else {
                k = 0;
            }

        }

    }

    private static boolean checkDuplicate(int[] arr, boolean flag) {
        int i = 0;

        while (i < arr.length - 1) {

            if (arr[i] == arr[i + 1]) {

                flag = true;

            } else {
                flag = false;
            }
            i++;

        }

        return flag;
    }

    private static int[] removeDuplicate(int[] arr) {

        int i = 0, j = 0;
        int[] temp = new int[arr.length];
        while (i < arr.length - 1) {

            if (arr[i] == arr[i + 1]) {

                temp[j] = arr[i + 1];
                i = i + 2;

            } else {

                temp[j] = arr[i];
                i = i + 1;

                if (i == arr.length - 1) {
                    temp[j + 1] = arr[i + 1];
                    break;
                }

            }
            j++;

        }
        System.out.println();
        return temp;
    }

}

Collectionクラスを使用せずに実装します。ただし、ループの改善はほとんど必要ありません。ボランティアの助けはかなりあります。上記の出力は次のようになります-> 2 3 4 6 8 10 11 12
Samrat Roy

これをより短い時間で実行するには、ハッシュベースのデータ構造を使用して重複を追跡する必要があります。そのため、HashSet()を使用する他のソリューションが表示されます。これはJavaに組み込まれています。
johnstosh 2017

@johnstoshはい私はそれについて知っていますが、コレクションを使用せずにそれを作ることを考えていたので、コメントで言及しています。2017年2月より前にコメントしているので、[コレクションをまったく使用する必要がないテクニックがあります。時間の複雑さが少ない] geeksforgeeks.org/….DSとアルゴのプラクティスを知らずにそのプログラムを試しました。そのために私に反対投票する必要はありません。
Samrat Roy

0
import java.util.Scanner;

public class OnlyDuplicates {
    public static void main(String[] args) {
        System.out.print(" Enter a set of 10 numbers: ");
        int[] numbers = new int[10];
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = input.nextInt();
        }
        numbers = onlyDuplicates(numbers);
        System.out.print(" The numbers are: ");
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i] + "");
        }
    }

    public static int[] onlyDuplicates(int[] list) {
        boolean flag = true;
        int[] array = new int[0];
        array = add2Array(array, list[0]);
        for (int i = 0; i < list.length; i++) {
            for (int j = 0; j < array.length; j++) {
                if (list[i] == array[j]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                array = add2Array(array, list[i]);
            }
            flag = true;
        }
        return array;
    }
    // Copy numbers1 to numbers2
    // If the length of numbers2 is less then numbers2, return false
    public static boolean copyArray(int[] source, int[] dest) {
        if (source.length > dest.length) {
            return false;
        }

        for (int i = 0; i < source.length; i++) {
            dest[i] = source[i];
        }
        return true;
    }
    // Increase array size by one and add integer to the end of the array
    public static int[] add2Array(int[] source, int data) {
        int[] dest = new int[source.length + 1];
        copyArray(source, dest);
        dest[source.length] = data;
        return dest;
    }
}

重複を返すためにこれを変更するには何が必要ですか?
ブレンデン2017年

これは新しい質問です。
willaien 2017年

0

これは、Setを使用せずに重複値を見つけるのに適した方法です。

public static <T> List<T> findDuplicates(List<T> list){

List<T> nonDistinctElements = new ArrayList<>();

  for(T s : list)
    if(list.indexOf(s) != list.lastIndexOf(s))
      if(!nonDistinctElements.contains(s))
        nonDistinctElements.add(s);

  return nonDistinctElements;
}

また、個別のリストを返すメソッドが必要だとします。つまり、要素が2回以上出現するリストを渡すと、個別の要素を含むリストが取得されます。

public static <T> void distinctList(List<T> list){

List<T> nonDistinctElements = new ArrayList<>();
for(T s : list)
  if(list.indexOf(s) != list.lastIndexOf(s))
    nonDistinctElements.add(s);

for(T nonDistinctElement : nonDistinctElements)
  if(list.indexOf(nonDistinctElement) != list.lastIndexOf(nonDistinctElement))
    list.remove(nonDistinctElement);
}

0

そして、commons-collections CollectionUtils.getCardinalityMapメソッドを使用するバージョン:

final List<Integer> values = Arrays.asList(1, 1, 2, 3, 3, 3);
final Map<Integer, Integer> cardinalityMap = CollectionUtils.getCardinalityMap(values);
System.out.println(cardinalityMap
            .entrySet()
            .stream().filter(e -> e.getValue() > 1)
            .map(e -> e.getKey())
            .collect(Collectors.toList()));

「」


0

このコードはどうですか-

public static void main(String[] args) {

    //Lets say we have a elements in array
    int[] a = {13,65,13,67,88,65,88,23,65,88,92};

    List<Integer> ls1 = new ArrayList<>();
    List<Integer> ls2 = new ArrayList<>();
    Set<Integer> ls3 = new TreeSet<>();

    //Adding each element of the array in the list      
    for(int i=0;i<a.length;i++) {
     {
    ls1.add(a[i]);
    }
    }

    //Iterating each element in the arrary
    for (Integer eachInt : ls1) {

    //If the list2 contains the iterating element, then add that into set<> (as this would be a duplicate element)
        if(ls2.contains(eachInt)) {
            ls3.add(eachInt);
        }
        else {ls2.add(eachInt);}

    }

    System.out.println("Elements in array or ls1"+ls1); 
    System.out.println("Duplicate Elements in Set ls3"+ls3);


}

0

重複と非重複の両方を含めたい場合のために。基本的には正解に似た答えですが、そうでない場合から戻るのではなく、他の部分を返します

このコードを使用(必要なタイプに変更)

public Set<String> findDup(List<String> Duplicates){
    Set<String> returning = new HashSet<>();
    Set<String> nonreturning = new HashSet<>();
    Set<String> setup = new HashSet<>();
    for(String i:Duplicates){
        if(!setup.add( i )){
            returning.add( i );
        }else{
            nonreturning.add( i );
        }
    }
    Toast.makeText( context,"hello set"+returning+nonreturning+" size"+nonreturning.size(),Toast.LENGTH_SHORT ).show();
    return nonreturning;
}

0

https://stackoverflow.com/a/52296246のバリアントとしてのより一般的な方法

    /**
     * Returns a duplicated values found in given collection based on fieldClassifier
     *
     * @param collection given collection of elements
     * @param fieldClassifier field classifier which specifies element to check for duplicates(useful in complex objects).
     * @param <T> Type of element in collection
     * @param <K> Element which will be returned from method in fieldClassifier.
     * @return returns list of values that are duplocated.
     */
    public static <T, K> List<K> lookForDuplicates(List<T> collection, Function<? super T, ? extends K> fieldClassifier) {

        return collection.stream().collect(Collectors.groupingBy(fieldClassifier))
                         .entrySet()
                         .stream()
                         .filter(e -> e.getValue().size() > 1)
                         .map(Map.Entry::getKey)
                         .collect(Collectors.toList());
    }

-1

最大値(<10000など)がわかっている場合は、速度のためにスペースを犠牲にすることができます。このテクニックの正確な名前を思い出せません。

疑似コード:

//does not handle case when mem allocation fails 
//probably can be extended to unknown values /larger values .
maybe by sorting first
public List<int> GetDuplicates(int max)
{   
    //allocate and clear memory to 0/false
    bit[] buckets=new bit[max]
    memcpy(buckets,0,max);
    //find duplicates
    List<int> result=new List<int>();
    foreach(int val in List)
    {
        if (buckets[val])
        {
            result.add(value);
        }
        else
        {
            buckets[val]=1;
        }
    }
    return  result
}

「ビット」ではなく「ブール」が欲しいと思いますか?投稿する前にコードを実行しましたか?これは良いスタートです。HashSet()を見てみると、それが目的の「バケット」実装であることがわかります。
johnstosh 2017

-1

ちょうどこれを試してください:

リストの値が次の場合の例:[1、2、3、4、5、6、4、3、7、8]重複するアイテム[3、4]。

Collections.sort(list);
        List<Integer> dup = new ArrayList<>();
        for (int i = 0; i < list.size() - 1; i++) {
            if (list.get(i) == list.get(i + 1)) {
                if (!dup.contains(list.get(i + 1))) {
                    dup.add(list.get(i + 1));
                }
            }
        }
        System.out.println("duplicate item " + dup);

ArrayList()でcontains()を呼び出すのは負荷の高い操作なので、代わりにSetの使用を検討する必要があります。HashSet()またはそれのリンクされたバージョンを使用する他のソリューションが表示されます。
johnstosh 2017
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.