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; +}; 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 +```