From e7bd5d5349f0252f79a04131a2fd237903caeee6 Mon Sep 17 00:00:00 2001 From: Gapur Kassym Date: Mon, 27 Jan 2020 12:38:22 +0600 Subject: [PATCH 1/2] Add solution on js for 1287 --- .../README.md | 22 +++++++++++++++++++ .../Solution.js | 16 ++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 solution/1287. Element Appearing More Than 25% In Sorted Array/README.md create mode 100644 solution/1287. Element Appearing More Than 25% In Sorted Array/Solution.js diff --git a/solution/1287. Element Appearing More Than 25% In Sorted Array/README.md b/solution/1287. Element Appearing More Than 25% In Sorted Array/README.md new file mode 100644 index 0000000000000..2808221424201 --- /dev/null +++ b/solution/1287. Element Appearing More Than 25% In Sorted Array/README.md @@ -0,0 +1,22 @@ +1287. Element Appearing More Than 25% In Sorted Array + +Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. + +```Javascript +const findSpecialInteger = function(arr) { + let count = 0; + let item = -1; + for (var i = 0; i < arr.length; i++) { + if (item == arr[i]) { + count++; + } else { + item = arr[i]; + count = 1; + } + if (count > arr.length * 0.25) { + return item; + } + } + return item; +}; +``` diff --git a/solution/1287. Element Appearing More Than 25% In Sorted Array/Solution.js b/solution/1287. Element Appearing More Than 25% In Sorted Array/Solution.js new file mode 100644 index 0000000000000..72e95cb15bf15 --- /dev/null +++ b/solution/1287. Element Appearing More Than 25% In Sorted Array/Solution.js @@ -0,0 +1,16 @@ +const findSpecialInteger = function(arr) { + let count = 0; + let item = -1; + for (var i = 0; i < arr.length; i++) { + if (item == arr[i]) { + count++; + } else { + item = arr[i]; + count = 1; + } + if (count > arr.length * 0.25) { + return item; + } + } + return item; +}; From 5eb6272e593709dc9473c15276ed598c5078e5a1 Mon Sep 17 00:00:00 2001 From: Gapur Kassym Date: Mon, 27 Jan 2020 12:42:43 +0600 Subject: [PATCH 2/2] update solution readme --- solution/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/solution/README.md b/solution/README.md index 4ecc21abe05f4..8e1702e40e060 100644 --- a/solution/README.md +++ b/solution/README.md @@ -1554,6 +1554,8 @@ │   └── Solution.cpp ├── 1278.Palindrome Partitioning III │   └── Solution.cpp +├── 1287.Element Appearing More Than 25% In Sorted Array +│   └── Solution.js ├── 5075.Number of Submatrices That Sum to Target │   └── Solution.py ├── 5076.Greatest Common Divisor of Strings @@ -1571,4 +1573,4 @@ ├── 5087.Letter Tile Possibilities │   └── Solution.py └── README.md -``` \ No newline at end of file +```