Question

If I were to implement a code that searchs for the first repeating pair in an...

If I were to implement a code that searchs for the first repeating pair in an array, this code would return the index of the first repeating pair. Using arrays and not array lists or sets.

for example; a=[1,2,3,4,2,1]; This code would return the index of the second 2 that was repeating. Output= 4. Please help

Homework Answers

Answer #1

public class lib{
public static void main(String[] args){
int a[]={1,2,3,4,2,1};
int pair[]=new int[2];
boolean flag=false;
for(int i=0;i<a.length-1;i++){
   pair[0]=a[i];
   pair[1]=a[i+1];
   for(int j=i+1;j<a.length-1;j++){
       if((a[j]==pair[0]&&a[j+1]==pair[1])||(a[j]==pair[1]&&a[j+1]==pair[0])){
           System.out.println("index of first repeating pair : "+j);
           flag=true;
           break;
       }
   }
   if(flag==true)break;
}
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr....
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 1.Design an algorithm to find all the common elements in two sorted lists of numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2,...
IN JAVA please Given a sorted array and a target value, return the index if the...
IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...
Create a function mult_arr which accepts two pointers-to-double as input along with an integer reflecting the...
Create a function mult_arr which accepts two pointers-to-double as input along with an integer reflecting the length of each array. The function should return void. Using pointer arithmetic to access the array elements, compute the element-wise product of the two arrays in-place (the result is stored in the first array). The function should return void. In main, read in two arrays of 10 values from the user following the I/O format seen in the example run below. On a new...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
MATLAB CODE: Matlab’s polyder() and polyint() functions return the derivative and integral of a polynomial, respectively....
MATLAB CODE: Matlab’s polyder() and polyint() functions return the derivative and integral of a polynomial, respectively. For example, polyder([4 -2 3 7 -5]) yields [16 -6 6 7] in other words: d/dx{4x4−2x3 +3x2+7x−5} = 16x3−6x2 +6x+7and,polyint([16 -6 6 7]) yields [4 -2 3 7 0] note the zero at the end. in other words:∫(16x3-6x2+6x+7)=4x4–2x3+ 3x2+7x+? where C = 0 As seen above, the constant of integration is always assumed to be zero when using polyint().While polyint() is available, using your...
Implement the following code in ARM on Raspberry Pi, compile and run g=12, h=8, i=2, j=5;...
Implement the following code in ARM on Raspberry Pi, compile and run g=12, h=8, i=2, j=5; f = (g + h) - (i + j); Your program displays the message: f = (g + h) – (i + j) = 13 Note that 13 should be calculated, not hardcoded E.g., When you change h = 18 for example, the output should be 23.
I need assistance writing this code. I keep generating errors and not sure what to do...
I need assistance writing this code. I keep generating errors and not sure what to do now. Design and implement a method that will take any value as String (even it's a number!) and convert that value to a number in any base. Use the method; private static ArrayList convertToBase(String value, int currentBase, int targetBase){ // your algorithm return result; //which is a String array } Examples: convertToBase("10011", 2, 10) should return [1, 9], meaning, (10011)base2 = (19)base10 convertToBase("100", 10,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT