Skip to content

Commit a8bce26

Browse files
Add implementation to check if an array is sorted and remove duplicates from an array
1 parent a7c671f commit a8bce26

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class arrayIsSorted {
2+
public static void main(String[] args) {
3+
int arr[] = { 1,100, 2, 3, 4, 2, 3, 7, 54, 9, 1 };
4+
// int arr[] ={1,2,3,4,5,6,7};
5+
boolean result = isSorted(arr);
6+
if (result) {
7+
System.out.println("yes the array is sorted");
8+
} else {
9+
System.out.println("array is not sorted");
10+
}
11+
12+
}
13+
14+
public static boolean isSorted(int arr[]) {
15+
16+
for(int i=1; i<arr.length;i++){
17+
if(arr[i] < arr[i-1]) return false;
18+
}
19+
return true;
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
import java.util.HashSet;
3+
import java.util.Set;
4+
5+
/* Remove Duplicates from Sorted Array */
6+
7+
public class removeDuplicateInArray {
8+
public static void main(String[] args) {
9+
int arr[] = { 1, 2, 3, 4, 1, 1, 1, 3, 4, 6, 7, 7, 7, 5 };
10+
Set<Integer> myset = new HashSet<>();
11+
for (int nums : arr) {
12+
myset.add(nums);
13+
}
14+
System.out.println("Array without duplicate Element : " +myset);
15+
}
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)