Question

Please write in C# Write a method in C# int[]GetDuplicatesIndices(int[]array), which takes an array of integers...

Please write in C#

Write a method in C# int[]GetDuplicatesIndices(int[]array), which takes an array of integers and returns an array that holds the indices of all the duplicates.

Example if the array has the following value 10, 5, 3, 7, 5, 11, 15, 13, 11, 6, 3, 19 then note that the value 5 has duplicate at index 4, value 3 has a duplicate at index 11, and value 7 has a duplicate at index 6. So the method should return an array containing 4, 11, 6.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
using System;

public class DuplicatesIndices
{
    public static int[] GetDuplicatesIndices(int[] array)
    {
        int count = 0;
        for (int i = 0; i < array.Length; i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (array[i]==array[j])
                {
                    count++;
                    break;
                }
            }
        }
        
        int[] indices = new int[count];
        int index = 0;
        for (int i = 0; i < array.Length; i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (array[i]==array[j])
                {
                    indices[index++] = i;
                    break;
                }
            }
        }

        return indices;
    }
    
    public static void Main(string[] args)
    {
        int[] arr = {10, 5, 3, 7, 5, 11, 15, 13, 11, 6, 3, 19};
        int[] indices = GetDuplicatesIndices(arr);
        for (int i = 0; i < indices.Length; i++)
        {
            Console.Write(indices[i] + " ");
        }

        Console.WriteLine();
    }
}
Add a comment
Know the answer?
Add Answer to:
Please write in C# Write a method in C# int[]GetDuplicatesIndices(int[]array), which takes an array of integers...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • A method called linearSearch(), which takes as parameters an array of int followed by three values...

    A method called linearSearch(), which takes as parameters an array of int followed by three values of type int, and returns a value of type int. The first int parameter represents a key, the second int parameter represents a starting position, and the third int parameter represents an end position. If the key occurs in the array between the start position (inclusive) and the end position (exclusive), the method returns the position of the first occurrence of the key in...

  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

  • In C, write a method that takes in an array of integers and returns its size....

    In C, write a method that takes in an array of integers and returns its size. Q9]Write a method that takes in an array of integers and returns it's sizel10 points

  • A function that takes three parameters: an int array, an int n that is the size...

    A function that takes three parameters: an int array, an int n that is the size of the array and an int pointer max that is to assign the maximum value in the array. The function will find and return the index of the first occurrence of the maximum value in the array and assign the maximum value to the pointer. For example, in the array {1, 2, 6, 5, 6, 4, 3, 6, 4}, the maximum value is 6...

  • Write a method that takes in a two-dimensional array of integers, do some calculation for each...

    Write a method that takes in a two-dimensional array of integers, do some calculation for each row in the two-dimensional array, and returns a one-dimensional array of sums for each row. Your code must work any array dimensions, including jagged arrays. The calculations are: • Sum of each row • Maximum value of each row • Minimum value of each row • Average of each row Here is an example an array passed and returns an array of sums for...

  • JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers,...

    JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers, and returns the sum of all the odd numbers in the array. The method should return 0 if the array contains no odd numbers. For example, if the given array is [12, 10, 5, 8, 13, 9] then the method should return 27 (5+13+9=27). Starter Code: public class Odds { public static int sumOdds(int[] numbers) { //TODO: complete this method    } }

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • Java Array The method rotateLeft() takes in a reference a to an integer array and a...

    Java Array The method rotateLeft() takes in a reference a to an integer array and a positive integer n and it returns a new array whose contents is the contents of the input array rotated to the left n places. So each element a[i] of the input array should be placed at location b[i-n] of the returned array. If the index i-n is negative, then that index should "wrap" around to the end of the output array. For example, if...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT