-1

Given and int array; int[] arr = {1, 2, 5, 8, 9, 10, 20, 80, 9, 0, 100, 90, 30, 55, 22, 87, 88, 22, 33, 22, 1, 2, 3};

Requirement: requirement is: there is an array, which is without sorted and with duplicates. created a method which can accept int[], and after ordered and removed duplicates, return it as an int[]...

My processing solution is:

static void removingDups4(int[] arr) {

    LinkedHashSet<Integer> setDups = new LinkedHashSet(Arrays.asList(arr));

    for (int each : arr) {
        setDups.add(each);
    }

    System.out.println(setDups);

    int[] newArr = new int[setDups.size()];
    int i = 0;
    while (i < setDups.size()) {

        for (int each : setDups) {
            newArr[i] +=  each;
            i++;
        }
        System.out.println(newArr);

    }

}

output:

[[I@77459877, 0, 1, 2, 3, 5, 8, 9, 10, 20, 22, 30, 33, 55, 80, 87, 88, 90, 100] Exception in thread "main" java.lang.ClassCastException: class [I cannot be cast to class java.lang.Integer ([I and java.lang.Integer are in module java.base of loader 'bootstrap')    at SearchingAnElementFromArray_BinarySearch.removingDups4(SearchingAnElementFromArray_BinarySearch.java:86)     at Array.SearchingAnElementFromArray_BinarySearch.main(SearchingAnElementFromArray_BinarySearch.java:12)

Process finished with exit code 1
G.S
  • 10,413
  • 7
  • 36
  • 52
UG_Coder
  • 9
  • 1
  • Does this answer your question? [Java Array Sort descending?](https://stackoverflow.com/questions/1694751/java-array-sort-descending) – Soni Sol Jul 13 '20 at 04:31

3 Answers3

2

This is one way to do it (Java8+ required):

public static int[] sortIntArrayNoDups(int[] array) {
    int[] tmp = java.util.stream.IntStream.of(array).distinct().toArray();
    Arrays.sort(tmp);
    return tmp;
}

To use:

int[] arr = {1, 2, 5, 8, 9, 10, 20, 80, 9, 0,
             100, 90, 30, 55, 22, 87, 88, 22, 
             33, 22, 1, 2, 3};
arr = sortNoDups(arr);
System.out.println(Arrays.toString(arr));

Output:

[0, 1, 2, 3, 5, 8, 9, 10, 20, 22, 30, 33, 55, 80, 87, 88, 90, 100]
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
  • What if you call .distinct() first and then sort it? If there are a lot of duplicates, then sorting will take a lot of unnecessary time. – Harshal Parekh Jul 13 '20 at 04:40
  • 1
    You can also do `return IntStream.of(array).distinct().sorted().toArray();` –  Jul 13 '20 at 05:43
  • See the code in [comment by saka1029](https://stackoverflow.com/questions/62869119/create-a-return-method-which-accepts-an-int-array-and-returns-ordered-and-a-non#comment111176924_62869314) run [live at IdeOne.com](https://ideone.com/zPoYL6). – Basil Bourque Jul 13 '20 at 06:11
2

Using Java-8:

static int[] removingDups4(int[] arr) {
    return 
        new ArrayList<>(
            Arrays
            .stream(arr)       // Generating a stream of the `int` values held in the array.
            .boxed()           // Auto-boxing `int` primitives to `Integer` objects.
            .collect( Collectors.toCollection(TreeSet::new) )  // Passing the `Integer` objects into a `TreeSet` to (a) eliminate duplicates, and (b) sort them.
        )
        .stream()
        .mapToInt(i -> i)
        .toArray();
}

Basically putting the numbers into a Set will remove the duplicates.

And using a SortedSet/NavigableSet like TreeSet will also sort the numbers.

See this code run live at IdeOne.com.

        int[] input = { 8 , 6 , 7 , 5 , 3, 0 , 9 , 8 };  // Repeating `8` at beginning and end. 
        int[] result = removingDups4( input ) ;
        System.out.println( Arrays.toString( result ) ) ;

[0, 3, 5, 6, 7, 8, 9]

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
0

Instead of using a LinkedHashSet you can use a TreeSet which sorts the elements in ascending order and removes the duplicates automatically. You can then convert the TreeSet into an object array using the toArray() method and then convert it to an integer array and return it. I'm not putting the code here because it is easy to figure it out.

  • static Integer[] orderingAnArrayAndRemovingDups2(int[] arr) { TreeSet set = new TreeSet(Arrays.asList(arr)); Integer[] newArr = new Integer[set.size()]; set.toArray(newArr); return newArr; }. i tried this way, but still got some exception. like: Exception in thread "main" java.lang.ClassCastException: class [I cannot be cast to class java.lang.Comparable ([I and java.lang.Comparable are in module java.base of loader 'bootstrap') – UG_Coder Jul 13 '20 at 11:31