-1

I have an arraylist which contains some values with duplicates i want to collect those values into another Arraylist.... like

 Arraylist<String> one;   //contains all values with duplicates
  one.add("1");
  one.add("2");
  one.add("2");
  one.add("2");

Here, I want to get all the duplicates values in another arraylist...

Arraylist<String> duplicates;    //contains all duplicates values which is 2.

I want those values which counts greater or equals 3.....

Currently, I don't have any solution for this please help me to find out

  • 1
    Please explain your problem with input and required output. – mukesh210 Jan 02 '19 at 13:30
  • Why a list with the duplicates? After all, the duplicate elements are all equal, it doesn't make much sense with elements of type `String`... Do you need all the duplicates, or just the ones that are repeated (i.e. all but the first occurrences)? Besides, you're not telling us when 2 elements are considered equal. Is it by means of the `equals` method or by some other criteria? Please clarify this question and also add sample input and expected output. – fps Jan 02 '19 at 14:03
  • Shared a generic utility to solve for a value n, for the number of occurrence of an element. Do [take a look at it](https://stackoverflow.com/a/54010424/1746118), hope it would help. – Naman Jan 02 '19 at 17:12

2 Answers2

6

You can use a set for this:

Set<String> set = new HashSet<>();
List<String> duplicates = new ArrayList<>();

for(String s: one) {
    if (!set.add(s)) {
        duplicates.add(s);
    }
}

You just keep adding all the elements to the set. If method add() returns false, this means the element was not added to set i.e it already exists there.

Input: [1, 3, 1, 3, 7, 6]

duplicates: [1, 3]

EDITED

For the value which counts 3 or greater, you can use streams to do it like so:

List<String> collect = one.stream()
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
            .entrySet()
            .stream()
            .filter(e -> e.getValue() >= 3)
            .map(Map.Entry::getKey)
            .collect(Collectors.toList());

Basically you collect you initial list in a map, where key is the string and value is the count. Then you filter this map for values that have count greater than 3, and collect it to the result list

Schidu Luca
  • 3,897
  • 1
  • 12
  • 27
  • thanks for the answer but can you please read the updated question and sorry for the last incomplete question..... –  Jan 03 '19 at 11:40
  • it is give me some errors like Lambada expression, Method references, Static Interface Method Calls are not supported in language 1.7 in these lines (e -> e.getValue() >= 3......Map.Entry::getKey.....Function.identity()) also require minimum Api level 24 –  Jan 04 '19 at 08:14
  • 1
    It means that you have java 7. In this version lambdas are not supported – Schidu Luca Jan 04 '19 at 08:25
  • actually i have java 8 but android studio supports java 7 now i set the compiler to 1.8 thanks but this answer is not suitable for me because it requires the minimum android version nougat –  Jan 04 '19 at 09:58
2

You could also do this via a stream:

List<String> duplicates = one.stream()
                .collect(Collectors.groupingBy(Function.identity(), counting()))
                .entrySet()
                .stream()
                .filter(e -> e.getValue() > 1)
                .map(Map.Entry::getKey)
                .collect(Collectors.toCollection(ArrayList::new));
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • I would prefer something like `one .stream() .distinct() .filter(i->Collections.frequency(one, i)>1) .collect(Collectors.toCollection(ArrayList::new));` – Eritrean Jan 02 '19 at 13:51
  • 3
    @Eritrean Using `Collections.frequency` for each element of the stream turns the complexity of the solution to `O(n^2)`, which is highly undesirable. – fps Jan 02 '19 at 13:58
  • please read the updated question and sorry for the last one. It is too complex to implement by the fresher can you give me an another answer or explain this....thanks for your contribution –  Jan 03 '19 at 11:43