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 ;
	}

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.