0

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

Edited by Viper588: n/a

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Viper588
0

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.

0

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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.