Hey guys im very new to java and am having some difficulty solveing a problem for my class. I have an array of 10 numbers and have to write a method that will eliminate any duplicates in the array and return that array. Iv been toying with different thing for a while and still cant get anything to work. Any help in the right direction would be very appreciated. Thank you

import java.util.*;

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

		Scanner joe = new Scanner(System.in);
		
	int[] numbers = new int[10];

	
		System.out.println("Enter 10 numbers");
		numbers[0] = joe.nextInt();
		numbers[1] = joe.nextInt();
		numbers[2] = joe.nextInt();
		numbers[3] = joe.nextInt();
		numbers[4] = joe.nextInt();
		numbers[5] = joe.nextInt();
		numbers[6] = joe.nextInt();
		numbers[7] = joe.nextInt();
		numbers[8] = joe.nextInt();
		numbers[9] = joe.nextInt();
	
 
		

		}
		public static int[] eliminateDuplicates(int[] numbers){

			Arrays.sort(numbers);
        
		 int left = 0;

		 int right = numbers.length - 1;

		 while( left < right ) {
       
        int temp = numbers[left];

        numbers[left] = numbers[right];

        numbers[right] = temp;

        left++;

        right--;



		 }}
		return ;
	}

Dani AI

Generated

A few practical clarifications and two simple implementations that follow 's "temporary array" idea and offer a Collections-based alternative. Arrays in Java are fixed-size, so the usual pattern is to build a new int[] sized to the number of unique values and return that. Sorting the input can help (duplicates become adjacent), but sorting alone — or reversing after a sort — does not remove duplicates; the algorithm must copy only the first occurrence of each value.

A compact approach that preserves the original input order uses LinkedHashSet:

public static int[] eliminateDuplicates(int[] numbers) {
    java.util.Set<Integer> seen = new java.util.LinkedHashSet<>();
    for (int n : numbers) seen.add(n);
    int[] result = new int[seen.size()];
    int i = 0;
    for (int n : seen) result[i++] = n;
    return result;
}

If the exercise forbids using Collections, a plain-O(n^2) method implements 's suggestion with a temporary array:

public static int[] eliminateDuplicates(int[] numbers) {
    int[] temp = new int[numbers.length];
    int unique = 0;
    for (int i = 0; i < numbers.length; i++) {
        int v = numbers[i];
        boolean found = false;
        for (int j = 0; j < unique; j++) if (temp[j] == v) { found = true; break; }
        if (!found) temp[unique++] = v;
    }
    int[] result = new int[unique];
    System.arraycopy(temp, 0, result, 0, unique);
    return result;
}

Troubleshooting notes: make sure the method returns an int[] (not void); decide whether original order must be preserved (LinkedHashSet or the manual scan) or whether sorting-first is acceptable (then copy adjacent uniques). Test edge cases (all-equal, empty array) and avoid mutating the caller's array unless that's intended.

Recommended Answers

All 2 Replies

Your eliminateDuplicates method is not even close to solving the problem. You need to compare 2 numbers in the array and if they are the same, you discard one. Create a temporary array that you can fill up with the unique values and then return this array when you are done checking all the numbers.

The method only has code to sort the array, which i then tried to use for eliminating duplicates but got nowhere near getting it right so i deleted what i had. Thank you for the suggestion!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.