Deprecated: Creation of dynamic property WPtouchProFour::$settings_object is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php on line 82

Deprecated: Automatic conversion of false to array is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/admin-load.php on line 70

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_classic_block is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 347

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 349

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$toolbar_block_side is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 350

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$panels_block is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 351

Deprecated: Creation of dynamic property Advanced_Editor_Tools::$used_block_buttons is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/tinymce-advanced/tinymce-advanced.php on line 354

Deprecated: Creation of dynamic property YARPP::$is_custom_template is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Core.php on line 56

Deprecated: Creation of dynamic property YARPP::$db_options is deprecated in /home2/learnera/public_html/tech/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Core.php on line 69

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home2/learnera/public_html/tech/wp-content/plugins/wptouch/core/class-wptouch-pro.php:82) in /home2/learnera/public_html/tech/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":727,"date":"2021-01-12T10:30:49","date_gmt":"2021-01-12T10:30:49","guid":{"rendered":"https:\/\/learnerandtutor.com\/?p=96"},"modified":"2021-01-12T10:30:49","modified_gmt":"2021-01-12T10:30:49","slug":"sales-by-match","status":"publish","type":"post","link":"http:\/\/tech.learnerandtutor.com\/sales-by-match\/","title":{"rendered":"Sales by Match"},"content":{"rendered":"

Problem<\/strong><\/p>\n

Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale.
Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.<\/p>\n

For example, there are n=7 socks with colors ar = [1,2,1,2,1,3,2] .
There is one pair of color 1 and one of color 2.
There are three odd socks left, one of each color. The number of pairs is 2.<\/p>\n

Function Description
Complete the sockMerchant function in the editor below.
It must return an integer representing the number of matching pairs of socks that are available.<\/p>\n

sockMerchant has the following parameter(s):
n: the number of socks in the pile
ar: the colors of each sock<\/p>\n

Input Format
The first line contains an integer n, the number of socks represented in ar.
The second line contains n space-separated integers describing the colors ar[i] of the socks in the pile.<\/p>\n

Constraints
1 <= n <= 100
1 <= ar[i] <= 100 where 0 <= i < n<\/p>\n

Output Format
Return the total number of matching pairs of socks that Alex can sell.<\/p>\n

Sample Input
9
10 20 20 10 10 30 50 10 20<\/p>\n

\"\"<\/figure>\n

Sample Output
3<\/p>\n

Alex can match three pairs of socks<\/p>\n

Solution<\/strong><\/p>\n

import java.util.HashMap;\nimport java.util.Scanner;\n\npublic class PairOfSocks {\n    static int sockMerchant(int n, int[] ar) {\n        HashMap<Integer, Integer> colorsMap = new HashMap<>();\n        for (int color : ar) {\n            colorsMap.put(color, (colorsMap.getOrDefault(color, 0)) + 1);\n        }\n        final int[] totalPair = {0};\n        colorsMap.forEach((key, value) -> totalPair[0] += value \/ 2);\n        return totalPair[0];\n    }\n\n    private static final Scanner scanner = new Scanner(System.in);\n\n    public static void main(String[] args) {\n        int n = scanner.nextInt();\n        scanner.skip(\"(\\r\\n|[\\n\\r\\u2028\\u2029\\u0085])?\");\n        int[] ar = new int[n];\n        String[] arItems = scanner.nextLine().split(\" \");\n        scanner.skip(\"(\\r\\n|[\\n\\r\\u2028\\u2029\\u0085])?\");\n\n        for (int i = 0; i < n; i++) {\n            int arItem = Integer.parseInt(arItems[i]);\n            ar[i] = arItem;\n        }\n        int result = sockMerchant(n, ar);\n        System.out.println(result);\n        scanner.close();\n    }\n}<\/pre>\n

Explanation<\/strong><\/p>\n

We need to find no of pairs for each number. We can add the quantity of each number into a map. Then we can divide the values by 2 and sum up to get the final answer.<\/p>\n","protected":false},"excerpt":{"rendered":"

Problem Alex works at a clothing store. There is a large pile of socks that must be paired by color for sale.Given an array of integers representing the color of each sock, determine how many pairs of socks with matching … Continue reading →<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":694,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[43],"_links":{"self":[{"href":"http:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/posts\/727"}],"collection":[{"href":"http:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/comments?post=727"}],"version-history":[{"count":0,"href":"http:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/posts\/727\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/media\/694"}],"wp:attachment":[{"href":"http:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/media?parent=727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/categories?post=727"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/tech.learnerandtutor.com\/wp-json\/wp\/v2\/tags?post=727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}