SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Arrays
Exercise:
Programming Projects
Question:4 | ISBN:9780132830317 | Edition: 5

Question

Write a static method called deleteRepeats that has a partially filled array of characters as a formal parameter and that deletes all repeated letters from the array. Because a partially filled array requires two arguments, the method should actually have two formal parameters: an array parameter and a formal parameter of type int that gives the number of array positions used. When a letter is deleted, the remaining letters are moved one position to fill in the gap. This creates empty positions at the end of the array so that less of the array is used. Because the formal parameter is a partially filled array, a second formal parameter of type int should tell how many array positions are filled. This second formal parameter cannot be changed by a Java method, so have the method return the new value for this parameter. For example, consider the following code:

char a[10];

a[0] = 'a';

a[1] = 'b';

a[2] = 'a';

a[3] = 'c';

int size = 4;

size = deleteRepeats(a, size);

After this code is executed, the value of a[0] is 'a', the value of a[1] is 'b', the value of a[2] is 'c', and the value of size is 3. (The value of a[3] is no longer of any concern, because the partially filled array no longer uses this indexed variable.) You may assume that the partially filled array contains only lowercase letters. Write a suitable test program for your method.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Implementation of deleteRepeats method:

public static int deleteRepeats(char arr[], int size)
{
	for(int i = 0; i < size; i++)
	{
		for(int j = i + 1; j < size; j++)
		{
			if(arr[i] == arr[j])
			{
				for(int k = j; k < size - 1; k++)
				{
					arr[k] = arr[k + 1];
					
				}

				size--;
				j--;
			}
		}
	}

	return size;
}

Program:

// DeleteRepeatsTest.java
public class DeleteRepeatsTest
{
	// Implementation of deleteRepeats method
	public static int deleteRepeats(char arr[], int size)
	{
		for(int i = 0; i < size; i++)
		{
			for(int j = i + 1; j < size; j++)
			{
				if(arr[i] == arr[j])
				{
					for(int k = j; k < size - 1; k++)
					{
						arr[k] = arr[k + 1];
						
					}

					size--;
					j--;
				}
			}
		}

		return size;
	}
	
	
	public static void main(String[] args)
	{
		 char[] a = new char[10];
		 
		 a[0] = 'a';
		 a[1] = 'b';
		 a[2] = 'a';
		 a[3] = 'c';		 
		 
		 int size = 4;
		 
		 System.out.println("Array before deleting duplicates:");
		 for(int i = 0; i < size; i++)
			 System.out.println("a[" + i + "] = " + a[i]);
		 
		 System.out.println("Size: " + size);
		
		 size = deleteRepeats(a, size);
		 
		 System.out.println("\nArray after deleting duplicates:");
		 for(int i = 0; i < size; i++)
			 System.out.println("a[" + i + "] = " + a[i]);	
		 
		 System.out.println("Size: " + size);		 
	}
}

Output:

Array before deleting duplicates:
a[0] = a
a[1] = b
a[2] = a
a[3] = c
Size: 4

Array after deleting duplicates:
a[0] = a
a[1] = b
a[2] = c
Size: 3

 

0 0

Discussions

Post the discussion to improve the above solution.