Skip to content

Commit 36b129a

Browse files
authored
Merge pull request #216 from Mcbencrafter/main
added array manipulation snippets
2 parents c9ae43c + dff0e93 commit 36b129a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Remove duplicates
3+
description: Removes duplicate elements from an list
4+
author: Mcbencrafter
5+
tags: list,duplicates,unique
6+
---
7+
8+
```java
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
12+
public static <T> List<T> removeDuplicates(List<T> list) {
13+
return list.stream()
14+
.distinct()
15+
.collect(Collectors.toList());
16+
}
17+
18+
// Usage:
19+
List<Integer> list = List.of(1, 2, 3, 4, 5, 1, 2, 3, 4, 5);
20+
List<Integer> result = removeDuplicates(list);
21+
System.out.println("List with duplicates removed: " + result); // [1, 2, 3, 4, 5]
22+
```

0 commit comments

Comments
 (0)