Question

In java6. Device Name System Suggested Problem Name: Device Name System Suggested Function Name: deviceNamesSystem...

In java


6. Device Name System

Suggested Problem Name: Device Name System

Suggested Function Name: deviceNamesSystem

Create unique device names to be used in a residential loT (Internet of Things) system. If a device name already exists in the system, an integer number is added at the end of the name to make it unique. The integer added starts with 1 and is incremented by 1 for each new request of an existing device name. Given a list of device name requests, process all requests and return an array of the corresponding unique device names.


Example

n=6

devicenames = ['switch', 'tv', 'switch', 'tv', 'switch', 'tv']


Function Description

Complete the function deviceNamesSystem in the editor below.

deviceNamesSystem has the following parameter(s):

string devicenames/n]: an array of device name strings in the order requested.

Returns

string [n] : an array of string usernames in the order assigned

6. Device Name System Suggested Problem Name: Device Name System Suggested Function Name: deviceNamesSystem Create unique dev

Sample Case 0 Sample Input STDIN Function devicenames[] size n = 4 devicenames=[mixer, toaster, mixer,tv] 4 mixer toa


12 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

//required method, copy and paste wherever you want

public static String[] deviceNamesSystem(String deviceNames[]) {

      // creating an array of same size to keep the results

      String uniqueNames[] = new String[deviceNames.length];

      // looping from i=0 to deviceNames.length-1

      for (int i = 0; i < deviceNames.length; i++) {

            // fetching device name at index i

            String name = deviceNames[i];

            // initially assuming this is not a duplicate value

            boolean duplicate = false;

            // looping and checking if this value exist previously

            for (int j = 0; j < i; j++) {

                  if (uniqueNames[j].equals(name)) {

                        // exists. setting duplicate to true and exiting inner loop

                        duplicate = true;

                        break;

                  }

            }

            // if this is no duplicate, adding unchanged name to uniqueNames

            // array

            if (!duplicate) {

                  uniqueNames[i] = name;

            } else {

                  // otherwise starting with id=1

                  int id = 1;

                  boolean exists = true;

                  // looping as long as exists is true

                  while (exists) {

                        // initially assuming device with this name does not exist

                        // already

                        exists = false;

                        // looping and checking the previous elements in uniqueNames

                        // for devicename: name+id (here + is for concatenation)

                        for (int j = 0; j < i; j++) {

                              if (uniqueNames[j].equals(name + id)) {

                                    // found, setting exists to true

                                    exists = true;

                                    // moving to next id

                                    id++;

                                    // exiting inner loop

                                    break;

                              }

                        }

                  }

                  // after the loop, adding name+id to the uniqueNames array

                  // example: if name is "switch", and id is 7, name+id returns

                  // "switch7"

                  uniqueNames[i] = name + id;

            }

      }

      //returning the array

      return uniqueNames;

}

Add a comment
Answer #2

String a[] = {"a","a","b","c","a","d","b","b","b"};
        String b[] = new String[a.length];
        int cnt = 0;
        for (int i = 0; i < a.length; i++) {
            cnt = 0;
            for (int j = 0; j < a.length; j++) {
                if(a[i] == a[j]) {
                     cnt++;
                     if(cnt-1 == 0) {
                         b[j] = a[i];
                     }else {
                         b[j] = a[i]+ (cnt-1);
                     }            
                }
            }
        }
        System.out.println(Arrays.asList(b));

source: Solved on self
answered by: Ninad
Add a comment
Answer #3

public static List<String> deviceNamesSystem(List<String> devicenames) {

            

            List<String> res=new ArrayList<>();

            

            HashMap<String,Integer> map=new HashMap<>();

            

            for(int i=0;i<devicenames.size();i++){

                    if(map.containsKey(devicenames.get(i))){

                            

                            String temp=devicenames.get(i)+""+map.get(devicenames.get(i));

                            res.add(temp);

                            map.put(devicenames.get(i), map.get(devicenames.get(i)) + 1);

                    }

                    else{

                            res.add(devicenames.get(i));

                            map.put(devicenames.get(i), 1);

                    }

                    

                    

            }

            

            

            

            

            return res;

}


answered by: prathyush
Add a comment
Know the answer?
Add Answer to:
In java6. Device Name System Suggested Problem Name: Device Name System Suggested Function Name: deviceNamesSystem...
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
  • Can someone code this asap? Use any language that you want. 2. Ancestral Names Given a...

    Can someone code this asap? Use any language that you want. 2. Ancestral Names Given a list of strings comprised of a name and a Roman numeral, sort the list first by name, then by decimal value of the Roman numeral. In Roman numerals, a value is not repeated more than three times. At that point, a smaller value precedes a larger value to indicate subtraction. For example, the letter I represents the number 1, and Vrepresents 5. Reason through...

  • Define the functions in Python 3.8 1. Write a function most frequent n that takes a...

    Define the functions in Python 3.8 1. Write a function most frequent n that takes a list of strings and an integer n, and that returns a dictionary where the keys are the top n most frequent unique words in the list, and the values are the frequency of each word: For example, most frequent n(text, 3) should return the dictionary {'is': 2, "the’: 3, 'of': 2}, and most frequent n(text, 2) could return either {'is': 2, 'the’: 3} or...

  • For C++ This is the information about the meal class 2-1. (24 marks) Define and implement...

    For C++ This is the information about the meal class 2-1. (24 marks) Define and implement a class named Dessert, which represents a special kind of Meal. It is to be defined by inheriting from the Meal class. The Dessert class has the following constructor: Dessert (string n, int co) // creates a meal with name n, whose type // is "dessert" and cost is co The class must have a private static attribute static int nextID ; which is...

  • Stuck on this computer science assignment Write a program that demonstrates binary searching through an array...

    Stuck on this computer science assignment Write a program that demonstrates binary searching through an array of strings and finding specific values in an corresponding parallel double array. In all cases your output should exactly match the provided solution.o. Provided files: Assignment.cpp - starter assignment with function prototypes companies.txt - file for program to read. earnings.txt - file for program to read. Input1.txt Input2.txt - Test these inputs out manually, or use stream redirection Input3.txt - These inputs are based...

  • Objective In this assignment, you will practice solving a problem using object-oriented programming and specifically, you...

    Objective In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will use the concept of object aggregation (i.e., has-a relationship between objects). You will implement a Java application, called MovieApplication that could be used in the movie industry. You are asked to implement three classes: Movie, Distributor, and MovieDriver. Each of these classes is described below. Problem Description The Movie class represents a movie and has the following attributes: name (of type String), directorName...

  • Develop a functional flowchart and then write a C++ program to solve the following problem. 1....

    Develop a functional flowchart and then write a C++ program to solve the following problem. 1. Create a text file named c1.txt and write your brand of computer (like Dell, HP, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your program will also read the brand of your computer from the keyboard. The process of the file creation (name of the file, mode for opening...

  • // READ BEFORE YOU START: // You are given a partially completed program that creates a...

    // READ BEFORE YOU START: // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, gender, class, standard, and roll_number. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // If you modify any of the given code, the return types, or the parameters, you...

  • C++ Simple Employee Tracking System

    This assignment involves creating a program to track employee information.  Keep the following information on an employee:1.     Employee ID (string, digits only, 6 characters)2.     Last name (string)3.     First Name (string)4.     Birth date (string as MM/DD/YYYY)5.     Gender (M or F, single character)6.     Start date (string as MM/DD/YYYY)7.     Salary per year (double)Thus you must create a class that has all of this, and get/set methods for each of these fields.  Note: The fields that are designated as string should use the string...

  • c++ problem You are asked to implement a car ordering system for Bobcats Auto Dealership. This...

    c++ problem You are asked to implement a car ordering system for Bobcats Auto Dealership. This dealership is brand new and only sells one brand of cars with three different models. However, a buyer can add options if they choose. Write a C++ program that allows the user to order a single car with different options. All the options available will be stored in a file called "options.txt" along with their cost. You may assume that the file will not...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

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